Removing/destroying child object that belongs_to 2 parents
class Concert < ActiveRecord::Base
has_many :documents, :uniq => true
accepts_nested_attributes_for :documents #???, :allow_destroy => true
class Artist < ActiveRecord::Base
has_one :document, :validate => true
accepts_nested_attributes_for :document #???, :allow_destroy => true
class Document < ActiveRecord::Base
belongs_to :artist
belongs_to :concert
I want to remove a document from concerts: Destroy it, if its only parent
is a concert, but set concert_id to nil and save document, if it also
belongs_to an artist. (and analogue idea from artist point of view)
I would like to either:
In artist and concert class intercept .marked_for_destruction? And stop
destruction of document if it is referenced by the other parent. How do I
do that?
Or even better:
Add a before_destroy callback in document class that checks if this
document has a (live) second parent, but then I'd need to know which class
(parent) is calling destroy, so I'd know which foreign key to remove. How
do I know which parent is calling destroy?
I have explored polymorphic associations, but I need the SAME document to
belong_to 2 parents.
Have lost the plot if it makes any difference, but for completeness sake:
Concert and Artist are in a has_many :through => :engagements association
No comments:
Post a Comment