-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
after_destroy isn't called on has_many through #7618
Comments
I might be wrong, but this seems to me like a technicality rather than a bug... You shouldn't call delete_all, because that strategy intentionally doesn't run the associated callback(s). It's also the default strategy that gets used when deleting associated records in a has_many relationship. I'm not sure about destroy_all. You can see the docs here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html (Deleting from Associations) How about |
Hi, the problem is in that I don't need to remove all object_tags, see http://oi46.tinypic.com/axymt.jpg
UPD: |
Heaven, |
@ashrafuzzaman Hi, I meant that those who don't need any callback could switch to HABTM, but now I am using has many through especially because I need those callbacks, unfortunately only the after_create one works. |
It's a problem I have since Rails 3. When it will be fixed it will break a lot of apps. |
If it will be fixed it will help to avoid a lot of ugly hacks (at least for me) :) |
Hi, is there any chance it will be fixed soon? How can it break existing apps? Perhaps there could be a compromise in introducing this as an additional option for the has many through association? E.g. :destroy => true or something. |
I had a similar situation earlier today. Digging in to ActiveRecord associations/has_many_through_association#delete_records, I saw that the default strategy of has_many_through is :delete_all. It only calls :destroy_all when we explicitly specify dependent: :destroy on the association. In your case, the following modification should work. Removing a tag from a project will then destroy the object_tag without destroying the tag itself. class Project < ActiveRecord::Base
has_many :object_tags, :as => :taggable, :dependent => :destroy
has_many :tags, :through => :object_tags, :dependent => :destroy
end Please let me know if that works for you, I don't have time to try it out. |
@hungryzi Hi, yes, thank you, I am also found the same and this is the exact way how I solved the problem. Btw, no need to specify has_many :object_tags, :as => :taggable
has_many :tags, :through => :object_tags, :dependent => :destroy But frankly saying it's not very obvious, I always tough that the "dependent" option, specified on So, right now |
Since The only solution I have found is to hook an |
+1 for doing something about the inconsistency between after_create and after_destroy on join models. It wasted time for my team this week. |
I can take a look if someone can create executable test case following this pattern. https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb |
This is what the From what I see, the possible fix for this is just add the callback Is this a viable fix? If so, I can add tests around this and push the fix. LMK |
Could you open a PR so we can discuss the solution. Thanks |
Isn't this behavior tested here https://github.com/rails/rails/blob/master/activerecord/test/cases/associations/has_many_through_associations_test.rb#L145? |
+1 for what @heaven said, I had the exact same expectation about where to put the :dependent => :destroy. |
Facing same issue.. |
Guys I'm considering this issue as stale and I'll close in a month or so. If you still having this issue please provide an example application or an executable issue. |
Well it's not really an issue but more of an inconsistency in the framework I would say I really hope this get fixed soon and avoid losing time to more people. |
So it sound more like a feature request and we don't accept feature requests in the issue tracker. The behavior can be changed, we only need a PR or someone on the core team wanting to work on this. I'm closing this issue now. |
I wrote a test case showing behavior difference between explicit destroy and implicit destroy (by reassigning) here https://github.com/edogawaconan/rails-hmt-after_destroy Considering http://guides.rubyonrails.org/association_basics.html actually mentioned this suggestion
I'd personally call this a bug instead of feature request. |
I'm with the same problem running Rails 3.2.16. @rafaelfranca I think you could re-open this issue since the docs are really clear about it and now you have an example from @edogawaconan to test it, just as you requested
|
@gerep Rails 3.2 is no longer supported. Can you reproduce the issue on 4.1 or master? If so, please open a new issue. |
issue: rails/rails#7618 The issue is circumvented in: vendor/engines/your_platform/app/models/active_record_associations_patches.rb trello: https://trello.com/c/cOa9EpLf/677-veranstaltungen
Added a warning for behavior when `:dependent` option is used in conjunction with `:through` option. Text is taken from http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many. I was recently confused by this behavior and overlooked the description in the docs. It might be nice to include this warning in the "Methods Added by `has_many`" section as well, but this is where I would have looked first. For additional context, see: * rails/rails#7618 * http://stackoverflow.com/questions/30629680/rails-isnt-running-destroy-callbacks-for-has-many-through-join-model
Here is the case:
The problem is in that the
ObjectTag.after_destroy
is never called when I am updating Project and unassigning tags. And only being called on Project/Tag .destroy or when manually calling project.tags.destroy(id).Why not to call destroy_all instead of delete_all? Or how to handle this in any other way? And why then after_create is being called? Something is wrong with this.
UPD:
Here is the
ProjectsController
:Should I manually synchronize project tags and remove each unassigned tag manually? I have 15 tagged models so performing this in each controller doesn't looks good.
The text was updated successfully, but these errors were encountered: