Ruby on rails 如何在活动记录中按特定条件订购?

Ruby on rails 如何在活动记录中按特定条件订购?,ruby-on-rails,activerecord,ruby-on-rails-4,rails-activerecord,rails-postgresql,Ruby On Rails,Activerecord,Ruby On Rails 4,Rails Activerecord,Rails Postgresql,要列出数据库中的所有商店,我需要查询 @shops = Shop.includes(:opening_times) .references(:opening_times) .limit(paging) .offset(@offset) 如果我只想得到现在营业的商店,我可以添加 .where("opening_times.day =?", today) #Open today .where("opening_times

要列出数据库中的所有商店,我需要查询

@shops = Shop.includes(:opening_times)
             .references(:opening_times)
             .limit(paging)
             .offset(@offset)
如果我只想得到现在营业的商店,我可以添加

.where("opening_times.day =?", today) #Open today
.where("opening_times.opens <= ?", @now)  #Opening time is before now
.where("(opening_times.opens+opening_times.open_for) > ?", @now)  #Closing time is after now
.order("opening_times.opens+opening_times.open_for") #Order by earliest opening time
.where(“开张_times.day=?”,今天)#今天开张
.where(“开始时间?”,@now)#结束时间在现在之后
.订单(“开盘次数.开盘+开盘次数.开盘时间”)#按最早开盘时间下单
但我需要的是列出所有的商店,在顶部列出目前正在营业的商店。有没有办法根据列表是否满足当前打开的条件对列表进行排序


这是使用postgres。

如果您将您的条件改为
order by
子句,会发生什么?我相信它应该还能用。@Teeg你是说像
.order(“opening_times.day=?”,今天)
?这给了我ActiveRecord::StatementInvalid:PG::SyntaxError:ERROR:输入端的语法错误。好的,一些DBMS允许您这样做,显然postgres不允许。发布您正在使用的数据库通常是好的,因为有些数据库允许某些事情,而其他数据库则不允许。我会考虑一下,如果可以的话再回来…@teeah我明白了,谢谢你。