【发布时间】:2016-11-30 15:52:26
【问题描述】:
我在 rails 上使用 c9 cloud ruby,我的 Rails 版本是 5.0.0.1
我想编写一个 ActiveRecord 查询来选择昨天没有去过的评分最高的餐厅。当我在查询中应用allwhere、order 和first 时,它会引发错误。但是,如果我不使用where 或order,它可以正常工作。
这行得通:
restaurant1= Restaurant.order('rating').last!
@suggested_restaurants=restaurant1.name
这会产生奇怪的结果:
restaurant1=Restaurant.order('rating').where{last_visit<Date.yesterday}
@suggested_restaurants=restaurant1
结果: #<ActiveRecord::QueryMethods::WhereChain:0x000000046af7f8>
此触发错误:
restaurant1=Restaurant.order('rating').where{last_visit<Date.yesterday}.last
@suggested_restaurants=restaurant1
错误: undefined method 'last' for #ActiveRecord::QueryMethods::WhereChain:0x00000004587a10>
我可以通过使用 find_by_sql 来解决这个问题,但我想知道为什么以及如何在 Rails 5 中使用 where。非常感谢!
【问题讨论】:
标签: ruby-on-rails rails-activerecord where-clause ruby-on-rails-5 active-record-query