有没有办法通过变量为 ActiveRecord 查询添加范围?
if @date.present?
result << Foo.in_region(params[:region_id]).created_since(@date)
result << Bar.in_region(params[:region_id]).created_since(@date)
result << Baz.in_region(params[:region_id]).created_since(@date)
else
result << Foo.in_region(params[:region_id])
result << Bar.in_region(params[:region_id])
result << Baz.in_region(params[:region_id])
end
我想做类似的事情:
scope = in_region(params[:region_id])
scope << created_since(@date) if @date.present?
%w(Foo Bar Baz).each do |model|
result << model.scope
end
当然,它不是这样工作的。