File tree 2 files changed +24
-1
lines changed
lib/active_record/associations 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -379,7 +379,9 @@ def load_target
379
379
if i
380
380
@target . delete_at ( i ) . tap do |t |
381
381
keys = [ "id" ] + t . changes . keys + ( f . attribute_names - t . attribute_names )
382
- t . attributes = f . attributes . except ( *keys )
382
+ f . attributes . except ( *keys ) . each do |k , v |
383
+ t . send ( "#{ k } =" , v )
384
+ end
383
385
end
384
386
else
385
387
f
Original file line number Diff line number Diff line change @@ -1282,4 +1282,25 @@ def test_include_method_in_has_many_association_should_return_true_for_instance_
1282
1282
comment = post . comments . build
1283
1283
assert post . comments . include? ( comment )
1284
1284
end
1285
+
1286
+ def test_load_target_respects_protected_attributes
1287
+ topic = Topic . create!
1288
+ reply = topic . replies . create ( :title => "reply 1" )
1289
+ reply . approved = false
1290
+ reply . save!
1291
+
1292
+ # Save with a different object instance, so the instance that's still held
1293
+ # in topic.relies doesn't know about the changed attribute.
1294
+ reply2 = Reply . find ( reply . id )
1295
+ reply2 . approved = true
1296
+ reply2 . save!
1297
+
1298
+ # Force loading the collection from the db. This will merge the existing
1299
+ # object (reply) with what gets loaded from the db (which includes the
1300
+ # changed approved attribute). approved is a protected attribute, so if mass
1301
+ # assignment is used, it won't get updated and will still be false.
1302
+ first = topic . replies . to_a . first
1303
+ assert_equal reply . id , first . id
1304
+ assert_equal true , first . approved?
1305
+ end
1285
1306
end
You can’t perform that action at this time.
0 commit comments