You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When calling deep_clone with include and referencing a has_and_belongs_to_many association - it doesn't actually make new instances of the object(s) like it does for every other association (has_many, etc).
Using the test suite methods:
deftest_single_include_associationdeep_clone=@jack.deep_clone(:include=>:mateys)assertdeep_clone.new_record?assertdeep_clone.saveassert_equal1,deep_clone.mateys.sizeputs"\n\nDEBUG DEBUG single include association"puts"@jack.matey_ids => #{@jack.matey_ids}"puts"deep_clone.matey_ids => #{deep_clone.matey_ids}"end
...
deftest_should_deep_clone_many_to_many_associations@human=Animal::Human.create:name=>'Michael'@human2=Animal::Human.create:name=>'Jack'@chicken1=Animal::Chicken.create:name=>'Chick1'@chicken2=Animal::Chicken.create:name=>'Chick2'@human.chickens << [@chicken1,@chicken2]@human2.chickens << [@chicken1,@chicken2]deep_clone_human=@human.deep_clone(:include=>:ownerships)assertdeep_clone_human.new_record?assertdeep_clone_human.saveassert_equal2,deep_clone_human.chickens.countputs"\n\nDEBUG DEBUG many_to_many"puts"deep_clone_human.chicken_ids => #{deep_clone_human.chicken_ids}"puts"@human.chicken_ids => #{@human.chicken_ids}"end/*> bundle exec rake test...DEBUG DEBUG single include association@jack.matey_ids => [22]deep_clone.matey_ids => [23] --> NEW ID, NEW INSTANCE ....DEBUG DEBUG many_to_manydeep_clone_human.chicken_ids => [1, 2]@human.chicken_ids => [1, 2] --> SAME INSTANCES, SAME IDS - NOT NEW INSTANCES*/
Is this an issue?
The text was updated successfully, but these errors were encountered:
Thank you for your interest! The example you specified is actually a hm-through, not habtm. I think it also exemplifies why it works like this: some would like that only new join records to the records (chickens) are created, some would like the whole association to be duplicated. When you define your habtm as a hm-through, I think you can choose between both:
# Only duplicates ownerships
@human.deep_clone(include: :ownerships)
# Duplicates ownerships AND chickens
@human.deep_clone(include: { ownerships: :chicken })
When calling
deep_clone
withinclude
and referencing ahas_and_belongs_to_many
association - it doesn't actually make new instances of the object(s) like it does for every other association (has_many, etc).Using the test suite methods:
Is this an issue?
The text was updated successfully, but these errors were encountered: