Removing an ActiveRecord Transitive Association

I have the following model:

class Organization < ActiveRecord::Base has_many :providers, :dependent => :destroy has_many :products, :through => :providers end class Provider < ActiveRecord::Base belongs_to :organization has_many :products, :inverse_of => :provider end class Product < ActiveRecord::Base belongs_to :provider, :inverse_of => :products end 

When I create an organization with a supplier (and without products) and then delete it with destroy:

 Organization.find(1).destroy 

Rails 3.0.x does NOT remove the associated provider that exits the nonexistent id_ organization. This is a strange behavior, I would expect that neither there nor the provider will be deleted (what I want to do).

I see that there is a transitive association has_many: products: through =>: providers - interestingly, the reason is why the supplier is not deleted.

Thanks for any help

Edit:

Good thing this has nothing to do with Rails, we have the following check in the Provider class

  def prevent_redhat_deletion if redhat_provider? errors.add(:base, _("Red Hat provider can not be deleted")) return false end true end 

and obviously i was deleting redhat_provider. For some reason, Rails will not fail.

+4
source share
1 answer

What you can also try changes: depend =: destroy, to: dependent =>: delete. See if providers are removed. If they do, there might be something with your deletion prevention code.

+1
source

All Articles