Hi,
I have a table collection and a table collectionorganisation. The
collectionorganisation table is used to define a hierarchy between
collections.
So, in the table collectionorganisation there is two foreign key :
parent_id and son_id.
In the collection model, I define the two following has_many with
dependent => destroy :
has_many :collectionorganisation, :foreign_key => “parent_id”,
:dependent => :destroy
has_many :collectionorganisation, :foreign_key => “son_id”, :dependent
=> :destroy
Unfortunatly, only the last depedent(son_id) is took into account. So
when a delete a collection that is present in the field son_id of a
record of collectionorganisation, then this record (in
collectionorganisation) is correctly deleted. But, if the collection is
present in the field parent_id, then nothing is done.
Someone can help me ?
Thanks in advance.
Grégory Pardoen wrote:
I have a table collection and a table collectionorganisation. The
collectionorganisation table is used to define a hierarchy between
collections.
So, in the table collectionorganisation there is two foreign key :
parent_id and son_id.
In the collection model, I define the two following has_many with
dependent => destroy :
has_many :collectionorganisation, :foreign_key => “parent_id”,
:dependent => :destroy
has_many :collectionorganisation, :foreign_key => “son_id”, :dependent
=> :destroy
Unfortunatly, only the last depedent(son_id) is took into account. So
when a delete a collection that is present in the field son_id of a
record of collectionorganisation, then this record (in
collectionorganisation) is correctly deleted. But, if the collection is
present in the field parent_id, then nothing is done.
Someone can help me ?
http://blog.hasmanythrough.com/2006/4/21/self-referential-through
For best results, you’ll want to name your tables according to Rails’
conventions.
For the Collection model, the table should be collections
. If the join
model is CollectionOrganization, the table should be
collection_organizations
.
–
Josh S.
http://blog.hasmanythrough.com
Josh S. wrote:
Grégory Pardoen wrote:
I have a table collection and a table collectionorganisation. The
collectionorganisation table is used to define a hierarchy between
collections.
So, in the table collectionorganisation there is two foreign key :
parent_id and son_id.
In the collection model, I define the two following has_many with
dependent => destroy :
has_many :collectionorganisation, :foreign_key => “parent_id”,
:dependent => :destroy
has_many :collectionorganisation, :foreign_key => “son_id”, :dependent
=> :destroy
Unfortunatly, only the last depedent(son_id) is took into account. So
when a delete a collection that is present in the field son_id of a
record of collectionorganisation, then this record (in
collectionorganisation) is correctly deleted. But, if the collection is
present in the field parent_id, then nothing is done.
Someone can help me ?
has_many :through - Self-referential has_many :through associations
For best results, you’ll want to name your tables according to Rails’
conventions.
For the Collection model, the table should be collections
. If the join
model is CollectionOrganization, the table should be
collection_organizations
.
–
Josh S.
http://blog.hasmanythrough.com
Thanks for those informations.
I didn’t think about :through .
And I agree for the use of ror’s convention.
I will try that.
Best regards,
Greg