Hi, I''ve been trying to read ActiveRecord tutorials etc. but haven''t figured out how to get the information I want. Let''s say I have a users who own one to many bookshelves and some of these bookshelves contain books. Now I would like to get a list of books that belong to user named Timeon: SELECT books.name FROM books, shelves, users WHERE books.shelve_id=shelves.id AND shelves.owner_id=users.id AND users.name=''Timeon''; The models would be something like this (from the top of my head, may not be correct): Class User < ActiveRecord::Base has_many :book_shelves end Class BookShelve < ActiveRecord::Base belongs_to :user has_many :books end Class Book < ActiveRecord::Base belongs_to :book_shelves end So, how to get a list of Book objects that belong to user named Timeon with a single line of code? Most examples I''ve seen first find the user, then they list the shelves etc. but I would expect something this trivial to be simple to achieve with a single call. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dheeraj Kumar
2011-Aug-18 16:08 UTC
Re: Trivial SQL query - how to do it with ActiveRecord?
A classic HABTM! Class User < ActiveRecord::Base has_many :books, :through => :book_shelves end Class BookShelve < ActiveRecord::Base belongs_to :user has_many :books end Class Book < ActiveRecord::Base belongs_to :user, :through => :book_shelves end You can get the list of books like so: Book.where( :user => { :name => ? }, params[:username] ) refer these for more: http://edgeguides.rubyonrails.org/association_basics.html#the-has_many-through-association http://edgeguides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables On Thu, Aug 18, 2011 at 4:30 PM, timeon <tarmountamo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''ve been trying to read ActiveRecord tutorials etc. but haven''t > figured out how to get the information I want. Let''s say I have a > users who own one to many bookshelves and some of these bookshelves > contain books. Now I would like to get a list of books that belong to > user named Timeon: > > SELECT books.name FROM books, shelves, users WHERE > books.shelve_id=shelves.id AND shelves.owner_id=users.id AND > users.name=''Timeon''; > > The models would be something like this (from the top of my head, may > not be correct): > > Class User < ActiveRecord::Base > has_many :book_shelves > end > > Class BookShelve < ActiveRecord::Base > belongs_to :user > has_many :books > end > > Class Book < ActiveRecord::Base > belongs_to :book_shelves > end > > So, how to get a list of Book objects that belong to user named Timeon > with a single line of code? Most examples I''ve seen first find the > user, then they list the shelves etc. but I would expect something > this trivial to be simple to achieve with a single call. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, On Aug 18, 7:08 pm, Dheeraj Kumar <a.dheeraj.ku...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> A classic HABTM!Yes indeed - and Thank You! Solved the problem. I got confused by the picture on the association basics -section: I thought Appointment was the high level item "owning" Patients and Physicians, like what one would draw in an UML diagram. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.