我想显示最后一个请求>=1.day.ago的第一个用户,然后添加其余用户
def self.default_scope
where("last_request >= ?", 1.day.ago) + where("last_request < ? OR last_request is null", 1.day.ago)
end
此代码引发此错误:
undefined method `merge' for []:Array
我怎么能那样做?
更新
错误栈
NoMethodError - undefined method `merge' for #<Array:0xd16ba90>:
activerecord (3.2.13) lib/active_record/relation.rb:503:in `with_default_scope'
activerecord (3.2.13) lib/active_record/relation.rb:167:in `exec_queries'
activerecord (3.2.13) lib/active_record/relation.rb:160:in `block in to_a'
activerecord (3.2.13) lib/active_record/explain.rb:34:in `logging_query_plan'
activerecord (3.2.13) lib/active_record/relation.rb:159:in `to_a'
will_paginate (3.0.5) lib/will_paginate/active_record.rb:127:in `block in to_a'
will_paginate (3.0.5) lib/will_paginate/collection.rb:96:in `create'
will_paginate (3.0.5) lib/will_paginate/active_record.rb:126:in `to_a'
更新2
我正在使用rails_admin,日期时间字段排序不正确
1)按升序排序
2)按降序排列
最佳答案
在您的特定情况下,可以将作用域与
default_scope where('last_request >= :time OR (last_request < :time OR last_request IS NULL)', time: 1.day.ago).order('last_request DESC')
UPD
这不是activerecord或railsadmin问题,更多信息
ORDER BY ASC with Nulls at the Bottom
Rails: Order with nulls last
关于ruby-on-rails - Rails合并2个查询结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22477934/