Permalink
Browse files

CollectionAssociation#merge_target_lists should write to the underlyi…

…ng attributes when copying, rather than using the assignment method
  • Loading branch information...
1 parent 6660bee commit 60b23ea1da1c119eaab6d07e5a03cf3b5d5577d4 @jonleighton jonleighton committed May 14, 2011
@@ -405,17 +405,14 @@ def merge_target_lists(persisted, memory)
mem_record = memory.delete(record)
if mem_record
- # FIXME: this call to record.attributes causes many NoMethodErrors
@jonleighton
jonleighton May 14, 2011 Ruby on Rails member

@tenderlove I ended up removing this comment in this commit because after it we are no longer calling record.attributes. However we are calling record.attribute_names. I wasn't sure what it meant. What does it mean, and is it still relevant?

@tenderlove
tenderlove May 14, 2011 Ruby on Rails member

When reading attributes, we were rescuing NoMethodErrors and delegating off somewhere. I can't remember if we're doing that anymore, so I guess I would deem this comment as not relevant.

- attributes = record.attributes
-
# Only try to assign attributes which exist on mem_record
- shared = mem_record.attribute_names & attributes.keys
+ shared = mem_record.attribute_names & record.attribute_names
# Don't try to assign the primary key, or attributes which have changed on mem_record
excluded = ["id"] + mem_record.changes.keys
(shared - excluded).each do |key|
- mem_record.send("#{key}=", attributes[key])
+ mem_record[key] = record[key]
end
mem_record
@@ -1445,4 +1445,14 @@ def test_to_a_should_dup_target
assert_not_equal target.object_id, ary.object_id
end
+
+ def test_merging_with_custom_attribute_writer
+ bulb = Bulb.new(:color => "red")
+ assert_equal "RED!", bulb.color
+
+ car = Car.create!
+ car.bulbs << bulb
+
+ assert_equal "RED!", car.bulbs.to_a.first.color
+ end
end
@@ -11,4 +11,8 @@ def record_scope_after_initialize
@scope_after_initialize = self.class.scoped
end
+ def color=(color)
+ self[:color] = color.upcase + "!"
+ end
+
end
@@ -90,6 +90,7 @@ def create_table(*args, &block)
t.integer :car_id
t.string :name
t.boolean :frickinawesome
+ t.string :color
end
create_table "CamelCase", :force => true do |t|

0 comments on commit 60b23ea

Please sign in to comment.