Easy:
# oh, the joy of lazy evaluation...
result = Record.where(true) # I'd like to do Record.all, but that fetches the records eagerly!
result = result.where("...") if params[:x].present?
result = result.where("...") if user.role?(:admin)
result = result.where("...") if params[:y].present?
By the way, don't try this in irb
: the "print" part of "read-eval-print" will force evaluation of the record set.
EDIT: I just figured out that instead of Record.where(true)
, you could use Record.scoped
. This won't work in Rails 4, though.