在我添加更多的关系到我的项目后,我收到这个跟随错误.在本地主机上,页面显示完好无损.但是,当我尝试在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的Tagging 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匹配的位置(通过act-as-taggable-on).
然而,在我添加了一个关系(与eventdeals而不是traveldeals相关)后,我开始得到pg错误.
以下是添加的关系:
trip.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事件?
谢谢.
解决方法
您的查询不符合Rails 3的标准,我不想给您一个构建不良的解决方案.
以下帖子回答您的问题并遵守Rails 3惯例.
Rails 3,ActiveRecord,PostgreSQL – “.uniq” command doesn’t work?