在我为我的项目添加更多关系后,我收到了这个跟随错误。在 localhost 上,页面显示得非常好。但是,尝试在 heroku 上查看时出现错误。
ActiveRecord::StatementInvalid (PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
这是我的 traveldeal 控制器:
def show
@traveldeal = @commentable = Traveldeal.find(params[:id])
@comments = Comment.all
@title = @traveldeal.title
@eventdeals = Eventdeal.tagged_with(@traveldeal.location_tag_list, :any => true, :order => 'RANDOM()', :limit => 3)
end
traveldeal/show.html
<% unless @eventdeals.blank? %>
<h1>Events in the area:</h1>
<% @eventdeals.each do |eventdeal| %>
<%= link_to eventdeal do %>
<!-- content -->
<% end %>
<% end %>
<% end %>
Heroku 日志
SELECT DISTINCT eventdeals.* FROM "eventdeals" JOIN taggings event_taggings_e9f0e2e ON event_taggings_e9f0e2e.taggable_id = eventdeals.id AND event_taggings_e9f0e2e.taggable_type = 'Eventdeal' WHERE (event_taggings_e9f0e2e.tag_id = 1 OR event_taggings_e9f0e2e.tag_id = 3 OR event_taggings_e9f0e2e.tag_id = 4 OR event_taggings_e9f0e2e.tag_id = 5) ORDER BY RANDOM()):
此代码有效,因此我只显示了 3 个与位置匹配的随机事件交易(通过 act-as-taggable-on)与 traveldeal。
但是,在我添加了一个关系(与 eventdeals 但与 traveldeals 无关)后,我开始收到 pg 错误。
以下是添加的关系:
行程.rb
class Trip < ActiveRecord::Base
has_many :eventdealtrips, :dependent => :destroy
has_many :eventdeals, :through => :eventdealtrips
end
eventdeal.rb
class Eventdeal < ActiveRecord::Base
has_many :eventdealtrips
has_many :trips, :through => :eventdealtrips, :dependent => :destroy
end
eventdealtrip.rb
class Eventdealtrip < ActiveRecord::Base
belongs_to :eventdeal
belongs_to :trip
end
关于如何仍然获得 3 个事件交易的随机数组的任何建议?
谢谢。