This project is archived and is in readonly mode.
has_one dependent nullify deletes associated object on change
-
Gregory Man
+1 I have same issue.
-
labria
Added a test to show the problem.
-
labria
I have found the source of it. The HasOneAssociation has a "replace" method. Inside it, it checks the dependent? method of the association, and if it finds it to be true, it destroys the old associated object.
unless @target.nil? || @target == obj if dependent? && !dont_save @target.destroy unless @target.new_record? @owner.clear_association_cache else @target[@reflection.primary_key_name] = nil @target.save unless @owner.new_record? || @target.new_record? end endThe problem is: the "dependent?" method only checks this:
def dependent? @reflection.options[:dependent] endSo, no matter what kind of :dependent key was used, it destroys the record. I'll try to add tests and fix the problem.
-
labria
- Tag changed from has_one, nullify to has_one, nullify, patch
Here's the patch fixing the problem. What still remains is the fact that :destroy or :delete do not actually matter, each time #destroy is called.
-
labria
Fixed the case when destroy was called instead of delete. Tests included.
-
Max Lapshin
+1 This should be fixed.
-
Gregory Man
+1 patch work for me
-
Roy Man
+1 it works for me
-
Pratik
- Assigned user set to Pratik
-
Pratik
- State changed from new to incomplete
I don't think the tests should be using stubbing here. Account has the following callback :
before_destroy do |account| if account.firm Account.destroyed_account_ids[account.firm.id] << account.id end true endSo that can be used here.
-
Pratik
Nevermind, I'll make the change and commit.
Thanks.
-
Repository
- State changed from incomplete to resolved
(from [984bc7a614852944808739fae09a654b6e62872e]) Ensure replacing has_one associations respects the supplied :dependent option. [#1305 has_one dependent nullify deletes associated object on change state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
