How can you do a conditional where clause using AREL How can you do a conditional where clause using AREL ruby ruby

How can you do a conditional where clause using AREL


It is possible to chain where statements

residentials = Residential.where(:is_active => true)residentials = residentials.where(:other_thing => true) if param_was_passed

This should work.

Make sure this is not the last line in a function call; repeat the residentials variable as the last line in that case. (As per @digger69 comment)


You could build up the hash and then provide it to the .where method. Something like:

h = { }h[:is_active] = trueh[:field_x] = true if param_was_passedresidentials = Residential.where(h)