This project is archived and is in readonly mode.

#1843 ✓resolved
Martin Andert

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

class Group < ActiveRecord::Base
  has_many :group_content_assignments, :dependent => :destroy
  has_many :assigned_contents, :through => :group_content_assignments

class Content < ActiveRecord::Base
  has_many :group_content_assignments, :dependent => :destroy

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,

Comments and changes to this ticket

  • Martin Andert

    Martin Andert February 2nd, 2009 @ 11:15 AM

    Forgot the Annotation model above:

    class Annotation < ActiveRecord::Base
      belongs_to :group_content_assignment

    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

    Martin Andert March 15th, 2009 @ 07:52 PM

    Commit 47bdf3bf40ec17e1f8ca1c0e3d7f697d0c4cd1bf added a destroy method to AssociationCollection. Now I can call

    group.assigned_contents.destroy content

    instead of calling

    group.assigned_contents.delete content

    This should fix it.

  • Eloy Duran

    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.


  • klkk

    klkk May 23rd, 2011 @ 03:08 AM

    • Importance changed from “” to “”

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=""></a>

People watching this ticket

Referenced by