Multiple :conditions complexity

Kevin E. Hunt
Tue, 30 Aug 2005 20:56:20 -0000

I''ve run into an issue with the complexity of generating the
:conditions
array using multiple, *optional* search parameters.  I have two search
terms: user_id and category.  Because they are optional, if not
specified the controller should not filter on that column name.

I essentially have to build the condition array for each possible search
combination.  It works but it is unnecessarily complex, especially when
I want to add a 3rd search parameter.

dif list   # filter_by_* is set by before_filter
    conditions = []
    if @filter_by_user && @filter_by_categories
      conditions << "user_id=? and category in (?)"
      conditions << @filter_by_user
      conditions << @filter_by_categories
    elsif @filter_by_user
      conditions << "user_id=?"
      conditions << @filter_by_account.id
    elsif @filter_by_categories
      conditions << "zipcode in (?)"
      conditions << @filter_by_categories
    else
      conditions << ''1'' # TODO: This is also stupid
    end

    @xyz_pages, @xyzs = paginate :xyz,
      :per_page => 10,
      :conditions => conditions,
      :order_by => "created_at desc"
end

I attempted to go the path of xyz.find_by_* but it looks like paginate
wants to take care of the query on its own.

I''m hoping someone has found a nice way to do this, or that :conditions
is smarter than I think it is.  Any ideas?

Kevin E. Hunt