Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/deep_cloneable/deep_clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def deep_clone(*args, &block)
&block
)

kopy[association_reflection.counter_cache_column] = 0 if association_reflection.has_cached_counter?
kopy.send("#{association}=", duped_object)
elsif !options[:skip_missing_associations]
raise ::DeepCloneable::AssociationNotFoundException, "#{self.class}##{association}"
Expand Down
2 changes: 1 addition & 1 deletion test/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Dove < Bird
end

class Planet < ActiveRecord::Base
has_many :birds
has_many :birds, :counter_cache => :birds_count
end

class Ownership < ActiveRecord::Base
Expand Down
1 change: 1 addition & 0 deletions test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

create_table :planets, :force => true do |t|
t.column :name, :string
t.column :birds_count, :integer, :default => 0, :null => false
end

create_table :birds, :force => true do |t|
Expand Down
14 changes: 14 additions & 0 deletions test/test_deep_cloneable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,20 @@ def test_should_skip_missing_associations
assert deep_clone_earth.birds.detect { |bird| bird.is_a?(Animal::Chicken) }.ownerships.any?
end

def test_should_reset_counter_cache
@earth = Animal::Planet.create :name => 'Earth'

assert_equal 0, @earth.birds_count

@dove1 = @earth.birds.create!(:name => 'Dovey 1')
@dove2 = @earth.birds.create!(:name => 'Dovey 2')

assert_equal 2, @earth.birds_count

deep_clone_earth = @earth.deep_clone(:include => :birds)
assert_equal 2, deep_clone_earth.birds_count
end

def test_should_deep_clone_with_block
deep_clone = @jack.deep_clone(:include => :parrot) do |original, kopy|
kopy.cloned_from_id = original.id
Expand Down