Modelling a parent-child relationship


#1

I’ve been googling this for the last day, and right now all I’ve got is
a headache. There are several articles on this - but they are not making
sense. One is using a field :association_foreign_key that I can’t find
in my documentation. I admit it, I’m lost.

Assume I have a Categories table. To make it simple, give it one field,
a name.
A category can be at the top level (no parents)
A category can be subordinate to another category (it’s child - and
obviously, the first category is the parent. This is a self-referential
many-to-many.

OK, I know that I need a join table - for grins, lets call that
Relationship with two fields: category_id and parent_id. This isn’t
carved in stone. If I need another layout - and it works - I’m happy as
I can be.

What models do I need, and how do I define these relationships? Can
anyone help here.

Thanks much
—Michael


#2

Michael S. wrote:

I’ve been googling this for the last day, and right now all I’ve got is
a headache. There are several articles on this - but they are not making
sense. One is using a field :association_foreign_key that I can’t find
in my documentation.

That’s not a field, it’s an option, and it’s clearly explained in the
Rails association docs.

I admit it, I’m lost.

Assume I have a Categories table. To make it simple, give it one field,
a name.
A category can be at the top level (no parents)
A category can be subordinate to another category (it’s child - and
obviously, the first category is the parent. This is a self-referential
many-to-many.

Are you sure you mean that it’s many-to-many? If that were so, a
category could have more than one parent. Is that what you want? Or do
you really mean a simple hierarchical tree?

Let me know, and perhaps I can be more help.

Best,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid


#3

On 18 June 2010 20:10, Marnen Laibow-Koser removed_email_address@domain.invalid wrote:

Michael S. wrote:

A category can be subordinate to another category (it’s child - and
obviously, the first category is the parent. This is a self-referential
many-to-many.

Are you sure you mean that it’s many-to-many? If that were so, a
category could have more than one parent. Is that what you want? Or do
you really mean a simple hierarchical tree?

+1
It does sound like you’re quite sure you’re expecting to have
many-children and many-parents - almost as if a self-referential
has_as_belongs_to_many is what you’re wanting - never tried it myself,
but would give it a bash on a test application if that’s what you’re
after.


#4

Marnen Laibow-Koser wrote:

Are you sure you mean that it’s many-to-many? If that were so, a
category could have more than one parent. Is that what you want? Or do
you really mean a simple hierarchical tree?

That is true. A category can exist in several places in the hierarchy.
It may appear (for example) under basic products AND under Personal.
In that case it has two parents. This is not as common as having
multiple children, but both situations are valid. Multiple parents,
multiple children.

Let me know, and perhaps I can be more help.

Thanks much.


#5

Michael S. wrote:

Michael P. wrote:

It does sound like you’re quite sure you’re expecting to have
many-children and many-parents - almost as if a self-referential
has_as_belongs_to_many is what you’re wanting - never tried it myself,
but would give it a bash on a test application if that’s what you’re
after.

has_and_belongs_to_many is quite simple when joining 2 different tables
(a join table is used). The problem here is that the join is to the
SAME table.

No problem. Read the docs for HABTM. You can have both ends be the
same table.

Best,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid


#6

Interesting to read:
http://guides.rails.info/association_basics.html#self-joins

Op 18 jun 2010, om 19:53 heeft Michael S. het volgende
geschreven:


#7

Michael P. wrote:

It does sound like you’re quite sure you’re expecting to have
many-children and many-parents - almost as if a self-referential
has_as_belongs_to_many is what you’re wanting - never tried it myself,
but would give it a bash on a test application if that’s what you’re
after.

has_and_belongs_to_many is quite simple when joining 2 different tables
(a join table is used). The problem here is that the join is to the
SAME table.