有没有更好的方法可以根据自定义模型方法查找记录?
def search
payments = all_payments.plans.order('payments.created_at DESC').select do |payment|
payment.plan == query
end
payments.paginate(pagination_params)
end
这会占用并加载整个表,因此查询速度非常慢。
payment.plan
是该模型的访问者。
我无法承受限制,因为它对数据库中的旧记录不公平。
有什么想法吗?谢谢!
答案 0 :(得分:0)
这个怎么样:
payments = all_payments.plans.where(plan: query).order('payments.created_at DESC').paginate(pagination_params)