我有这个代码:
def build_query(options)
query = User
query = query.where('first_condition')
query = query.where('second_condition')
query = query.where('items.category_id = ?', category) if (options[:category])
if options[:location]
query = query.where('users.location = ?', options[:location])
end
query = query.order('users.current_sign_in_at DESC')
query = query.limit(MAX_RESULTS)
return query
end
不幸的是,每次我这样做都会触发一个请求query = query.where(...)
我可以链接所有 where 子句 ( where().where()...
),但是我如何保留我if
的 s ?
有没有办法告诉 ActiveRecord 不要触发查询?