This code doesnt feel right. I’m trying to make my controllers skinny and I feel like there is way too much logic in the controller action.
What would be a much better way of organising this code?
def search
where_obj = {:status => 1}
if params[:city].present?
where_obj.merge! :city => params[:city]
end
if params[:county].present?
where_obj.merge! :county => params[:county]
end
## THERE WILL BE MANY MORE IF STATEMENTS HERE DUE TO GROWING SEARCH FORM
@person = Person.where(where_obj)
end
As the search form grows this controller action will grow also. How can I keep my controller skinny?
How about:
Or a somewhat simpler version using the
slicemethod provided byActiveSupport(in Rails):If you’re going to have a bunch of different params to selectively include, then you could group them like this:
(Thanks @ajcodez for pointing out the need for a splat on
search_paramswhen passed toslice.)