Forum: Ruby on Rails Searching parent objects and associated objects with one search

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
551607f1d4bdd9ebf303e57cd8cdac45?d=identicon&s=25 Yogi (Guest)
on 2008-10-17 10:02
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark James (Guest)
on 2008-10-17 11:03
(Received via mailing list)
Yogi wrote:
>   belongs_to :book
>
> 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
551607f1d4bdd9ebf303e57cd8cdac45?d=identicon&s=25 Yogi (Guest)
on 2008-10-19 11:43
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2008-10-20 22:30
(Received via mailing list)
Yogi wrote:
>         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
This topic is locked and can not be replied to.