在我的rails代码中,我需要根据记录的日期和记录收到的投票的组合来对表进行查询。我完成它就像在轨道下面:如何将多个Rails SQL查询合并到一个单独的查询中?
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
效率不谈,我不能在组合查询结果使用分页更不用说代码是非常难看。什么是正确的方法来做到这一点?
在此先感谢。
真棒,这导致查询50%的性能增益。谢谢 –