Skip to content

Possible unexpected has_and_belongs_to_many behaviour. #137

@somazx

Description

@somazx

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:

def test_single_include_association
    deep_clone = @jack.deep_clone(:include => :mateys)
    assert deep_clone.new_record?
    assert deep_clone.save
    assert_equal 1, deep_clone.mateys.size
    puts "\n\nDEBUG DEBUG single include association"
    puts "@jack.matey_ids => #{@jack.matey_ids}"
    puts "deep_clone.matey_ids => #{deep_clone.matey_ids}"
end
 ...
def test_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)
    assert deep_clone_human.new_record?
    assert deep_clone_human.save
    assert_equal 2, deep_clone_human.chickens.count
    puts "\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_many
deep_clone_human.chicken_ids => [1, 2]
@human.chicken_ids => [1, 2]           --> SAME INSTANCES, SAME IDS - NOT NEW INSTANCES
*/

Is this an issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions