has_many question

57 views
Skip to first unread message

Dave Castellano

unread,
Nov 11, 2013, 2:02:18 PM11/11/13
to rubyonra...@googlegroups.com
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/.

Robert Walker

unread,
Nov 11, 2013, 6:08:40 PM11/11/13
to rubyonra...@googlegroups.com
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 }

Dave Castellano

unread,
Nov 12, 2013, 8:41:32 AM11/12/13
to rubyonra...@googlegroups.com
Thanks Robert,

Unfortunately, a minisection can belong to many books, chapters, ect...
so needs a join table and can't be flattened.

Colin Law

unread,
Nov 12, 2013, 8:55:19 AM11/12/13
to rubyonra...@googlegroups.com
On 12 November 2013 13:41, Dave Castellano <li...@ruby-forum.com> 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

Dave Castellano

unread,
Nov 13, 2013, 6:49:00 PM11/13/13
to rubyonra...@googlegroups.com
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?

Simeon Manolov

unread,
Nov 14, 2013, 3:16:20 AM11/14/13
to rubyonra...@googlegroups.com
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
--
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-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
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.

Dave Castellano

unread,
Nov 14, 2013, 8:06:21 PM11/14/13
to rubyonra...@googlegroups.com
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
>
> Simeon


Thank you. I'll give it a try.
Reply all
Reply to author
Forward
0 new messages