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

Dave C. 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 }

Thanks Robert,

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

Dave

On 12 November 2013 13:41, Dave C. [email protected] 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

Posted by Colin L. (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 L. (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?

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

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.

Dave

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs