This project is archived and is in readonly mode.

#6207 ✓stale
Doug Petkanics

has_many :through after_add goes through parent relationship, but after_remove does not

Reported by Doug Petkanics | December 22nd, 2010 @ 03:47 PM

In the case of a has_many :through relationship, which also happens to be polymorphic in both directions, the after_add callback gets executed for both the direct relationship and the through relationship. It seems like there is an inconsistency though, as the after_remove callback only gets executed on the direct relationship and not the through relationship. This is best shown by the following example where there are polymorphic types Locations and People. User being and instance of Person and Address being an instance of Location.

# location_person.rb
class LocationPerson < ActiveRecord::Base
  belongs_to :location, :polymorphic => true
  belongs_to :person, :polymorphic => true

# address.rb
class Address < ActiveRecord::Base
  has_many :people, :as => :location, :class_name => "LocationPerson"

# user.rb
class User < ActiveRecord::Base
  has_many :locations, :as => :person, :class_name => "LocationPerson", :after_remove => :remove_location_point, :after_add => :add_location_point
  has_many :addresses, :through => :locations, :source => :location, :source_type => "Address", :after_remove => :remove_address_point, :after_add => :add_address_point

  def remove_location_point(location)
    puts "Removing Location Point"

  def remove_address_point(address)
    puts "Removing Address Point"

  def add_location_point(location)
    puts "Adding Location Point"

  def add_address_point(address)
    puts "Adding Address Point"

Call the following in console to demonstrate the asymetry:

ruby-1.8.7-p302 > user = User.create
 => #<User id: 2, created_at: "2010-12-22 15:44:14", updated_at: "2010-12-22 15:44:14"> 
ruby-1.8.7-p302 > addr = Address.create
 => #<Address id: 2, created_at: "2010-12-22 15:44:23", updated_at: "2010-12-22 15:44:23"> 
ruby-1.8.7-p302 > user.addresses << addr
Adding Location Point
Adding Address Point
 => [#<Address id: 2, created_at: "2010-12-22 15:44:23", updated_at: "2010-12-22 15:44:23">] 
ruby-1.8.7-p302 > user.addresses = []
Removing Address Point
 => []

As you can see, "Adding Location Point" and "Adding Address Point" were both printed indicating both after_add callbacks were executed. However only "Removing Address Point" was printed indicating that the after_remove callback on the :locations relationship was never called.

Comments and changes to this ticket

  • rails

    rails March 23rd, 2011 @ 12:00 AM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails March 23rd, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

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