Merge branch 'dougo_clear-new-record-on-synchronize'
This commit is contained in:
commit
5f0e220038
|
@ -137,8 +137,8 @@ class ActiveRecord::Base
|
||||||
#
|
#
|
||||||
# # Example synchronizing unsaved/new instances in memory by using a uniqued imported field
|
# # Example synchronizing unsaved/new instances in memory by using a uniqued imported field
|
||||||
# posts = [BlogPost.new(:title => "Foo"), BlogPost.new(:title => "Bar")]
|
# posts = [BlogPost.new(:title => "Foo"), BlogPost.new(:title => "Bar")]
|
||||||
# BlogPost.import posts, :synchronize => posts
|
# BlogPost.import posts, :synchronize => posts, :synchronize_keys => [:title]
|
||||||
# puts posts.first.new_record? # => false
|
# puts posts.first.persisted? # => true
|
||||||
#
|
#
|
||||||
# == On Duplicate Key Update (MySQL only)
|
# == On Duplicate Key Update (MySQL only)
|
||||||
#
|
#
|
||||||
|
|
|
@ -43,6 +43,10 @@ module ActiveRecord # :nodoc:
|
||||||
instance.clear_aggregation_cache
|
instance.clear_aggregation_cache
|
||||||
instance.clear_association_cache
|
instance.clear_association_cache
|
||||||
instance.instance_variable_set '@attributes', matched_instance.attributes
|
instance.instance_variable_set '@attributes', matched_instance.attributes
|
||||||
|
# Since the instance now accurately reflects the record in
|
||||||
|
# the database, ensure that instance.persisted? is true.
|
||||||
|
instance.instance_variable_set '@new_record', false
|
||||||
|
instance.instance_variable_set '@destroyed', false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -52,4 +56,4 @@ module ActiveRecord # :nodoc:
|
||||||
self.class.synchronize(instances, key)
|
self.class.synchronize(instances, key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -134,8 +134,18 @@ describe "#import" do
|
||||||
let(:new_topics) { Build(3, :topics) }
|
let(:new_topics) { Build(3, :topics) }
|
||||||
|
|
||||||
it "reloads data for existing in-memory instances" do
|
it "reloads data for existing in-memory instances" do
|
||||||
Topic.import(new_topics, :synchronize => new_topics, :synchronize_key => [:title] )
|
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
|
||||||
assert new_topics.all?(&:new_record?), "Records should have been reloaded"
|
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "synchronizing on destroyed records with explicit conditions" do
|
||||||
|
let(:new_topics) { Generate(3, :topics) }
|
||||||
|
|
||||||
|
it "reloads data for existing in-memory instances" do
|
||||||
|
new_topics.each &:destroy
|
||||||
|
Topic.import(new_topics, :synchronize => new_topics, :synchronize_keys => [:title] )
|
||||||
|
assert new_topics.all?(&:persisted?), "Records should have been reloaded"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue