Yogi
2008-Oct-16 19:29 UTC
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Mark James
2008-Oct-17 09:03 UTC
Re: Searching parent objects and associated objects with one search
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]}%"] -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yogi
2008-Oct-19 09:42 UTC
Re: Searching parent objects and associated objects with one search
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Oct-20 20:30 UTC
Re: Searching parent objects and associated objects with one search
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] -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---