Hi all, I need some guidance please. I have several tables related by "has_many" and I would like to find the "distant children". Tables: Subjects Books Chapters Sections Subsections Minisections They are all related heirarchichally, so book for example is related to subjects and chapters the following way. All are related the same way. Book belongs_to :subject has_many :chapters So if I have the subject id, how can I find all the Minisections belonging to that subject? Thanks, Dave -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6dabe1fc293d6af97c6f9b0b597bfcce%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Dave Castellano wrote in post #1126985:> Hi all, > > I need some guidance please. > > I have several tables related by "has_many" and I would like to find the > "distant children". > > Tables: > Subjects > Books > Chapters > Sections > Subsections > Minisections > > They are all related heirarchichally, so book for example is related to > subjects and chapters the following way. All are related the same way. > Book > belongs_to :subject > has_many :chapters > > So if I have the subject id, how can I find all the Minisections > belonging to that subject?Database normalization is a good thing... until it isn''t. I fear you''ve run into one of those situations where de-normalization might be very useful for the sake of efficiency. Instead of doing a fully hierarchical structure you could flatten parts of it. For example your Minisection could have id, subject_id, book_id, chapter_id, section_id, subsection_id and then: Minisection belongs_to :subject belongs_to :book belongs_to :chapter belongs_to :section belongs_to :subsection Now you can find any list mini-section without using any joins in any way you see fit. minisections = Minisection.where({ subject: my_subject, book: my_book, chapter: my_chapter, section: my_section, subsection: my_subsection } -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c4bf1a0cb88cd96d391cfca4ec5c21a2%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Thanks Robert, Unfortunately, a minisection can belong to many books, chapters, ect... so needs a join table and can''t be flattened. Dave -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bafa0da1da17c14b3f8f7e745fe3fcaa%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On 12 November 2013 13:41, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: Please quote the previous message so that the thread can be followed. This is a mailing list not a forum, though you may be accessing it via a forum like interface> Unfortunately, a minisection can belong to many books, chapters, ect... > so needs a join table and can''t be flattened.That does not make sense, in your original post you implied minisection belongs_to subsection, and so on up through the heirarchy. Therefore any particular minisection is associated with only one subsection, section, chapter and so on. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLt2K8OUMMcEOOfbv-rpvH%2Bbwoh392QRH8j1sNCFdgNhuw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Posted by Colin Law (Guest) on 2013-11-12 14:56 Please quote the previous message so that the thread can be followed. This is a mailing list not a forum, though you may be accessing it via a forum like interface>Thanks. Did not know this. I see it in the form of a forum.Unfortunately, a minisection can belong to many books, chapters, ect...> so needs a join table and can''t be flattened.Posted by Colin Law (Guest) on 2013-11-12 14:56 That does not make sense, in your original post you implied minisection belongs_to subsection, and so on up through the heirarchy. Therefore any particular minisection is associated with only one subsection, section, chapter and so on. So, original question... Having several tables related by "has_many" and would like to find the "distant children". eg: Tables: Subjects Books Chapters Sections Subsections Minisections They are all related heirarchichally, so book for example is related to subjects and chapters the following way. All are related the same way. Book belongs_to :subject has_many :chapters So if I have the subject id, how can I find all the Minisections belonging to that subject without adding all related table id''s in each table? -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/90f8b1d69ff472137ce8164abc1b451b%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
The below example allows you to jump 2 levels below ''Subject''. Probably you can try to nest these (I cannot test this right now): class Subject has_many :books has_many :chapters, through: :books end now subject.chapters should work. Try going further down the hierarchy Simeon -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Dave Castellano Sent: Monday, November 11, 2013 9:02 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] has_many question Hi all, I need some guidance please. I have several tables related by "has_many" and I would like to find the "distant children". Tables: Subjects Books Chapters Sections Subsections Minisections They are all related heirarchichally, so book for example is related to subjects and chapters the following way. All are related the same way. Book belongs_to :subject has_many :chapters So if I have the subject id, how can I find all the Minisections belonging to that subject? Thanks, Dave -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6dabe1fc293d6af97c6f9b0b5 97bfcce%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/010201cee111%24cd45f250%2467d1d6f0%24%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Simeon Manolov wrote in post #1127415:> The below example allows you to jump 2 levels below ''Subject''. Probably > you > can try to nest these (I cannot test this right now): > > class Subject > has_many :books > has_many :chapters, through: :books > end > > now subject.chapters should work. Try going further down the hierarchy > > SimeonThank you. I''ll give it a try. Dave -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be1bf028a6958e689499e098835731c6%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.