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.
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?
+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.
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.
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.
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.