在我的项目中添加了更多的关系之后,我得到了这个follow错误。在本地主机上,页面显示得非常好。但是,当我试图在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个随机事件处理,它们与traveldeal匹配(通过充当taggable on)。
然而,在我添加了一个关系(与eventdeals相关,但与traveldeals无关)之后,我开始得到pg错误。
以下是添加的关系:
行程.rb
class Trip < ActiveRecord::Base
  has_many :eventdealtrips, :dependent => :destroy
  has_many :eventdeals, :through => :eventdealtrips
end

事件处理.rb
class Eventdeal < ActiveRecord::Base
  has_many :eventdealtrips
  has_many :trips, :through => :eventdealtrips, :dependent => :destroy
end

事件处理.rb
class Eventdealtrip < ActiveRecord::Base
  belongs_to :eventdeal
  belongs_to :trip
end

有什么建议可以告诉我怎样才能得到3个事件的随机数组吗?
谢谢。

最佳答案

我有这个问题,并通过在菊花链的末尾运行order来解决它。

10-06 03:09