【发布时间】:2014-01-10 04:35:18
【问题描述】:
在我的 rails 代码中,我需要根据记录日期和记录收到的投票的组合对表运行查询。我在 rails 中像下面这样完成它:
if params[:sort_by] == "default"
objs1 = class_name.where("created_at between '#{Date.today - 7}' and '#{Date.today}' and net_votes > 0").order("net_votes DESC")
objs2 = class_name.where("created_at between '#{Date.today - 30}' and '#{Date.today - 7}' and net_votes > 0").order("net_votes DESC")
objs3 = class_name.where("created_at between '#{Date.today - 90}' and '#{Date.today - 30}' and net_votes > 0").order("net_votes DESC")
objs4 = class_name.where("created_at < '#{Date.today - 90}' and net_votes > 0").order("net_votes DESC")
objs = objs1 + objs2 + objs3 + objs4
抛开效率不谈,我不能在组合查询结果上使用分页,更不用说代码非常难看。这样做的正确方法是什么?
提前致谢。
【问题讨论】:
标签: mysql sql ruby-on-rails postgresql rails-activerecord