Say i have multiple tables (models) and I need to select an item in one table, and all of the data in the other models to get a compete row. What''s the best way to join them so that I get the data I need in one query? I know I can say: p = Person.find_by_id(100, :include => [ :books]); If Person and Book have a "has_many" relationship, But what if Person has Books and Books has a has_many relationship to Categories and I want both models to be loaded at the same time? This is easy in other languages, but what''s the proper "Rails way" of doing this? -john --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
p = Person.find_by_id(100, :include => [ {:books => :categories} ]); John Adams wrote:> Say i have multiple tables (models) and I need to select an item in > one table, and all of the data in the other models to get a compete row. > > What''s the best way to join them so that I get the data I need in one > query? > > I know I can say: > > p = Person.find_by_id(100, :include => [ :books]); > > If Person and Book have a "has_many" relationship, But what if Person > has Books and Books has a has_many relationship to Categories and I > want both models to be loaded at the same time? > > This is easy in other languages, but what''s the proper "Rails way" of > doing this? > > > -john > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Can this be repeated indefinitely ? as in... p = Person.find_by_id(100, :include => [ {:books => { :categories => :subcategories }} ]); Thanks, -john On Oct 21, 2007, at 1:41 PM, William Pratt wrote:> >> p = Person.find_by_id(100, :include => [ {:books => :categories} ]); > > > > John Adams wrote: >> Say i have multiple tables (models) and I need to select an item in >> one table, and all of the data in the other models to get a >> compete row. >> >> What''s the best way to join them so that I get the data I need in one >> query? >> >> I know I can say: >> >> p = Person.find_by_id(100, :include => [ :books]); >> >> If Person and Book have a "has_many" relationship, But what if Person >> has Books and Books has a has_many relationship to Categories and I >> want both models to be loaded at the same time? >> >> This is easy in other languages, but what''s the proper "Rails way" of >> doing this? >> >> >> -john >> >> >> >>> >> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You bet :) John Adams wrote:> Can this be repeated indefinitely ? > > as in... > > p = Person.find_by_id(100, :include => [ {:books => { :categories > => :subcategories }} ]); > > Thanks, > -john > > On Oct 21, 2007, at 1:41 PM, William Pratt wrote: > > >>> p = Person.find_by_id(100, :include => [ {:books => :categories} ]); >>> >> >> John Adams wrote: >> >>> Say i have multiple tables (models) and I need to select an item in >>> one table, and all of the data in the other models to get a >>> compete row. >>> >>> What''s the best way to join them so that I get the data I need in one >>> query? >>> >>> I know I can say: >>> >>> p = Person.find_by_id(100, :include => [ :books]); >>> >>> If Person and Book have a "has_many" relationship, But what if Person >>> has Books and Books has a has_many relationship to Categories and I >>> want both models to be loaded at the same time? >>> >>> This is easy in other languages, but what''s the proper "Rails way" of >>> doing this? >>> >>> >>> -john >>> >>> >>> >>> >> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Within reason - the database query gets a little more gnarly each time. You might also eventually run into limits on the number of joins allowed in a single query You also want to be careful when loading multiple sibling has_many, eg Person.find_by_id(123, :include => [:books, :magazines]). If a person had 100 books and 100 magazines then this would end up with rails handling 10000 rows of results (100x100), which it currently does so in a not very speedy way. Fred On 21 Oct 2007, at 22:31, William Pratt wrote:> You bet :) > > John Adams wrote: >> Can this be repeated indefinitely ? as in... p = Person.find_by_id >> (100, :include => [ {:books => { :categories >> => :subcategories }} ]); Thanks, -john On Oct 21, 2007, at 1:41 >> PM, William Pratt wrote: >>>> p = Person.find_by_id(100, :include => [ {:books >>>> => :categories} ]); >>> John Adams wrote: >>>> Say i have multiple tables (models) and I need to select an item >>>> in one table, and all of the data in the other models to get a >>>> compete row. What''s the best way to join them so that I get the >>>> data I need in one query? I know I can say: p = Person.find_by_id >>>> (100, :include => [ :books]); If Person and Book have a >>>> "has_many" relationship, But what if Person has Books and Books >>>> has a has_many relationship to Categories and I want both models >>>> to be loaded at the same time? This is easy in other languages, >>>> but what''s the proper "Rails way" of doing this? -john > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
True, nothing is free, but within reason, this works very well. I do wish there was a way to limit the columns returned through select, and from what I have read, that is a future planned feature, but unfortunately, currently, you get every column from all tables. When he said indefinitely, I assumed he realized that building 10,000 objects would take a bit of time, but I guess I assume a little to much sometimes. :) Frederick Cheung wrote:> Within reason - the database query gets a little more gnarly each > time. You might also eventually run into limits on the number of > joins allowed in a single query > You also want to be careful when loading multiple sibling has_many, eg > > Person.find_by_id(123, :include => [:books, :magazines]). > > If a person had 100 books and 100 magazines then this would end up > with rails handling 10000 rows of results (100x100), which it > currently does so in a not very speedy way. > > Fred > > On 21 Oct 2007, at 22:31, William Pratt wrote: > > >> You bet :) >> >> John Adams wrote: >> >>> Can this be repeated indefinitely ? as in... p = Person.find_by_id >>> (100, :include => [ {:books => { :categories >>> => :subcategories }} ]); Thanks, -john On Oct 21, 2007, at 1:41 >>> PM, William Pratt wrote: >>> >>>>> p = Person.find_by_id(100, :include => [ {:books >>>>> => :categories} ]); >>>>> >>>> John Adams wrote: >>>> >>>>> Say i have multiple tables (models) and I need to select an item >>>>> in one table, and all of the data in the other models to get a >>>>> compete row. What''s the best way to join them so that I get the >>>>> data I need in one query? I know I can say: p = Person.find_by_id >>>>> (100, :include => [ :books]); If Person and Book have a >>>>> "has_many" relationship, But what if Person has Books and Books >>>>> has a has_many relationship to Categories and I want both models >>>>> to be loaded at the same time? This is easy in other languages, >>>>> but what''s the proper "Rails way" of doing this? -john >>>>> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I guess I should have added a qualifier, as in "within reason" to my query ;) It''s unfortunate that there''s no way to specify limit clauses to the subquery. (or is there? I know that :limit => 5 works on most finder methods.) -john On Oct 21, 2007, at 3:36 PM, William Pratt wrote:> True, nothing is free, but within reason, this works very well. I > do wish there was a way to limit the columns returned through > select, and from what I have read, that is a future planned > feature, but unfortunately, currently, you get every column from > all tables. When he said indefinitely, I assumed he realized that > building 10,000 objects would take a bit of time, but I guess I > assume a little to much sometimes. :) > > Frederick Cheung wrote: >> Within reason - the database query gets a little more gnarly each >> time. You might also eventually run into limits on the number of >> joins allowed in a single query >> You also want to be careful when loading multiple sibling >> has_many, eg >> >> Person.find_by_id(123, :include => [:books, :magazines]). >> >> If a person had 100 books and 100 magazines then this would end up >> with rails handling 10000 rows of results (100x100), which it >> currently does so in a not very speedy way. >> >> Fred >> >> On 21 Oct 2007, at 22:31, William Pratt wrote: >> >> >>> You bet :) >>> >>> John Adams wrote: >>> >>>> Can this be repeated indefinitely ? as in... p = Person.find_by_id >>>> (100, :include => [ {:books => { :categories >>>> => :subcategories }} ]); Thanks, -john On Oct 21, 2007, at 1:41 >>>> PM, William Pratt wrote: >>>> >>>>>> p = Person.find_by_id(100, :include => [ {:books >>>>>> => :categories} ]); >>>>>> >>>>> John Adams wrote: >>>>> >>>>>> Say i have multiple tables (models) and I need to select an item >>>>>> in one table, and all of the data in the other models to get a >>>>>> compete row. What''s the best way to join them so that I get the >>>>>> data I need in one query? I know I can say: p = Person.find_by_id >>>>>> (100, :include => [ :books]); If Person and Book have a >>>>>> "has_many" relationship, But what if Person has Books and Books >>>>>> has a has_many relationship to Categories and I want both models >>>>>> to be loaded at the same time? This is easy in other languages, >>>>>> but what''s the proper "Rails way" of doing this? -john >>>>>> >> >> >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
William Pratt wrote:> I do > wish there was a way to limit the columns returned through select, and > from what I have read, that is a future planned feature, but > unfortunately, currently, you get every column from all tables.Won''t the :select option give you this? Eric --~--~---------~--~----~------------~-------~--~----~ 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 22 Oct 2007, at 09:47, Eric Anderson wrote:> > William Pratt wrote: >> I do >> wish there was a way to limit the columns returned through select, >> and >> from what I have read, that is a future planned feature, but >> unfortunately, currently, you get every column from all tables. > > Won''t the :select option give you this?Currently, :include overwrites :select Fred> > Eric > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Oct 22, 1:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 Oct 2007, at 09:47, Eric Anderson wrote: > > > > > William Pratt wrote: > >> I do > >> wish there was a way to limit the columns returned through select, > >> and > >> from what I have read, that is a future planned feature, but > >> unfortunately, currently, you get every column from all tables. > > > Won''t the :select option give you this? > > Currently, :include overwrites :select > > Fred > > > > > Ericyou could try this (I haven''t myself http://assertbuggy.blogspot.com/2007/05/activerecord-select-with-include.html http://code.google.com/p/ar-select-with-include/ --~--~---------~--~----~------------~-------~--~----~ 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 Oct 22, 4:51 am, gene tani <gene.t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 22, 1:59 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > On 22 Oct 2007, at 09:47, Eric Anderson wrote: > > > > William Pratt wrote: > > >> I do > > >> wish there was a way to limit the columns returned through select, > > >> and > > >> from what I have read, that is a future planned feature, but > > >> unfortunately, currently, you get every column from all tables. > > > > Won''t the :select option give you this? > > > Currently, :include overwrites :select > > > Fred > > > > Eric > > you could try this (I haven''t myself > > http://assertbuggy.blogspot.com/2007/05/activerecord-select-with-incl...http://code.google.com/p/ar-select-with-include/BTW i htink the correct patch referred to on the Google code page http://dev.rubyonrails.org/ticket/7147 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---