Searching parent objects and associated objects with one search

Three simple models setup with :through association

Person   has_many :readings   has_many :books, :through => :readings

Reading   belongs_to :person   belongs_to :book

Book   has_many :readings   has_many :persons, :through => :readings

I want to search persons names and all his books names and return matching persons. how do i correctly form a search like this syntatically wrong one

Person.find(:all, :conditions => ["person.name LIKE ? and book.name LIKE ?", "%#{params[:person_name]}%", "%#{params[:book_name]}%"])

tia, yogi

Yogi wrote:

Three simple models setup with :through association

Person   has_many :readings   has_many :books, :through => :readings

Reading   belongs_to :person   belongs_to :book

Book   has_many :readings   has_many :persons, :through => :readings

I want to search persons names and all his books names and return matching persons. how do i correctly form a search like this syntatically wrong one

Person.find(:all, :conditions => ["person.name LIKE ? and book.name LIKE ?", "%#{params[:person_name]}%", "%#{params[:book_name]}%"])

That looks syntactically correct. It just needs a little tweaking:

Person.find(:all, :select => 'DISTINCT people.*', :joins => :books, :conditions => ["people.name LIKE ? AND books.name LIKE ?",                  "%#{params[:person_name]}%", "%#{params[:book_name]}%"]

Thanks for Mark for the 'tuning'. Now that I think of this, can I add another associated table (reviews) and include that into the search also? What I mean is that if the models are

Person         has_many :readings         has_many :books, :through => :readings     has_many :reviews

Reading         belongs_to :person         belongs_to :book

Book         has_many :readings         has_many :persons, :through => :readings

Review     belongs_to :person

Now I would like to form a search like below

Person.find(:all,       :conditions => ["people.name LIKE ? AND                books.name LIKE ? AND               reviews.rating LIKE ?",               "%#{params[:person_name]}%",               "%#{params[:book_name]}%",               "%#{params[:rating]}%"])

How would I use :joins to make the above to work?

tia, yogi

Yogi wrote:

Thanks for Mark for the 'tuning'. Now that I think of this, can I add another associated table (reviews) and include that into the search also? What I mean is that if the models are

Person         has_many :readings         has_many :books, :through => :readings     has_many :reviews

Reading         belongs_to :person         belongs_to :book

Book         has_many :readings         has_many :persons, :through => :readings

Review     belongs_to :person

Now I would like to form a search like below

Person.find(:all,       :conditions => ["people.name LIKE ? AND                books.name LIKE ? AND               reviews.rating LIKE ?",               "%#{params[:person_name]}%",               "%#{params[:book_name]}%",               "%#{params[:rating]}%"])

How would I use :joins to make the above to work?

Use the :joins option just like :include. So

   :joins => [:books, :reviews]