I am wondering if any one could explain table relationships for me using rails. What I am trying to do requires the joining of a product and the comments for the product, with the user information of whom posted the comment. So I have a products table that references a comments table that is referenced by the product_id and the product_type, and the comments table gets user information from the users table by a user_id. So right now, I am familiar with has_<something> and belongs_to, but don''t know how to make it work with what I am trying to do, and thus the further explanation. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On 7/8/07, Scott Pn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I am wondering if any one could explain table relationships for me using > rails. What I am trying to do requires the joining of a product and the > comments for the product, with the user information of whom posted the > comment. So I have a products table that references a comments table > that is referenced by the product_id and the product_type, and the > comments table gets user information from the users table by a user_id. > So right now, I am familiar with has_<something> and belongs_to, but > don''t know how to make it work with what I am trying to do, and thus the > further explanation. > > -- > Posted via http://www.ruby-forum.com/. > > > >Hey Scott, That''s a pretty easy set up. You won''t have to do any joins, Rails will take care of everything for you :) Anyway, now that you''ve got the tables set up, you can use AR''s has_* and belongs_to methods to set up the associations. class Product has_many :comments end class Comment belongs_to :user end You can set up the reverse associations if you want/need to. This code will let you do something like: p = Product.find 1 p.comments.first.user.username # ''dopey'' comments of course is the collection of all comments, and for each comment you have access to the user object. Now one problem is that if you have 1 product with 10 comments, you''ll end up making 21 db calls. 1 for the product itself, 10 for each of the comments, and 10 for the associated user. Fortunately Rails lets you eager load the assocations. p = Product.find 1, :include => :comments will take care of the join for you to include comments, so they''re fetched along with the product. You can also go as deep as you need: p = Product.find 1, :include => [ :comments => :user ] This will fetch all the data you need in one query. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for all of that. But how would I create relationships where i use a foreign key in the has_many and belongs_to declarations? So for instance if I automatically wanted to return all the comments and the users of the comments without having to use the :include parameter? -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
On 7/9/07, Scott Pn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Thanks for all of that. But how would I create relationships where i use > a foreign key in the has_many and belongs_to declarations? So for > instance if I automatically wanted to return all the comments and the > users of the comments without having to use the :include parameter?I''m not sure what you mean, honestly. has_many and belongs_to rely on there being a foreign key. The comments table would have a product_id foreign key to products, as well as a user_id key to users. Having those keys and the corresponding has_many and belongs_to statements set up the relationship to be used programmatically. I don''t know how you would automatically load the associations without :include. Why do you want to? The best I think you could do is to write a new finder or override the default finder, this way you can encapsulate the call. class Product < ActiveRecord::Base def self.heavy_find(id, options = {}) find(id, { :include => [ :comments => :users ] }.merge(options)) end OR def self.find(id, options = {}) super(id, { :include => [ :comments => :users ] }.merge(options)) end end *shrug* Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 7/9/07, Scott Pn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Thanks for all of that. But how would I create relationships where i use > a foreign key in the has_many and belongs_to declarations? So for > instance if I automatically wanted to return all the comments and the > users of the comments without having to use the :include parameter? > > -- > Posted via http://www.ruby-forum.com/. > > > >Funny, http://agilewebdevelopment.com/plugins/includebydefault was *just* published to the rails plugins repository. Looks like it ought to do exactly what you want. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pat Maddox wrote:> On 7/9/07, Scott Pn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> > Funny, http://agilewebdevelopment.com/plugins/includebydefault was > *just* published to the rails plugins repository. Looks like it ought > to do exactly what you want. > > Pato, awesome, thanks for the find. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---