在我的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%的性能增益。謝謝 –