This project is archived and is in readonly mode.
Calling delete on a has_many :through association does not call destroy on the association object
Reported by Martin Andert | February 2nd, 2009 @ 11:03 AM | in 2.x
Here is an example:
class GroupContentAssignment < ActiveRecord::Base
belongs_to :group
belongs_to :content, :counter_cache => true
has_many :annotations, :dependent => :destroy
end
class Group < ActiveRecord::Base
has_many :group_content_assignments, :dependent => :destroy
has_many :assigned_contents, :through => :group_content_assignments
end
class Content < ActiveRecord::Base
has_many :group_content_assignments, :dependent => :destroy
end
If I do the following...
group = Group.first
content = Content.first
group.assigned_contents << content
... an instance of GroupContentAssignment is created and saved and the counter_cache field on Content is incremented. But when removing...
group.assigned_contents.delete content
... no call to the destroy method of GroupContentAssignment is made and so destroy is not called for all its annotations and the counter_cache of the belonging Content is not decremented.
See also: #1196, http://railsforum.com/viewtopic....
Comments and changes to this ticket
-
Martin Andert February 2nd, 2009 @ 11:15 AM
Forgot the Annotation model above:
class Annotation < ActiveRecord::Base belongs_to :group_content_assignment end
To make things clearer...
group.assigned_contents.delete content
... deletes the association record in the db using SQL, but doesn't call destroy on its AR instance, so all annotations aren't destroyed (although declared with dependent => destroy).
-
Martin Andert March 15th, 2009 @ 07:52 PM
Commit 47bdf3bf40ec17e1f8ca1c0e3d7f697d0c4cd1bf added a
destroy
method toAssociationCollection
. Now I can callgroup.assigned_contents.destroy content
instead of calling
group.assigned_contents.delete content
This should fix it.
-
Eloy Duran March 16th, 2009 @ 09:54 AM
- State changed from new to resolved
I'm closing this, since that's probably what you meant to say, even though you reported a problem with the current behaviour to #2146. As I commented on that ticket, please open a new ticket with for the specific problem for any further discussion.
Thanks
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 2146 AutosaveAssociation doesn't run removing association callbacks PS: It's probably better to move any further discussion i...