I'm pretty new to mysql and need to build a search function that
searches fields in a 'case' object as well as fields in 'person'
that reference each 'case' object. The function must return a single
array of 'case' objects.
Searching for fields in the case object seems pretty straightforward:
@cases = Case.find(:all, :conditions => ["lower(field_1) like ? OR
lower(field_2) like ?", "%" + params[:search].downcase + "%", "%" +
params[:search].downcase + "%"])
render :partial => 'cases', :collection => @cases
But I can't figure out how to search objects that 'belong_to' the case
You can do Case.find :all, :include => :person, :conditions =>
['cases.foo = ? or people.bar = ?', ...]
In rails 2 you can use :joins instead of :include if you're not
interested in actually eager loading the :person associations.
And you've always been able to Case.find :all, :joins => "some fancy
bit of sql to join the people table in the desired way", :conditions
=> ['cases.foo = ? or people.bar = ?', ...]
(you almost always want to specify a :select option if you do tha)