Ticket #8277: add_reload_to_active_record_class_patch.diff
| File add_reload_to_active_record_class_patch.diff, 2.5 kB (added by zdennis, 1 year ago) |
|---|
-
test/base_test.rb
old new 1248 1248 t2.reload 1249 1249 assert_equal t1.title, t2.title 1250 1250 end 1251 1252 def test_reload_multiple 1253 t1 = Topic.find(1) 1254 t2 = Topic.find(2) 1255 t1.title = "something else" 1256 t2.title = "something entirely new" 1257 1258 Topic.connection.execute( "UPDATE #{Topic.table_name} 1259 SET title='something else' 1260 WHERE id=1", 1261 "Updating records without ActiveRecord" ) 1262 Topic.connection.execute( "UPDATE #{Topic.table_name} 1263 SET title='something entirely new' 1264 WHERE id=2", 1265 "Updating records without ActiveRecord" ) 1266 1267 Topic.reload( [t1, t2] ) 1268 assert_equal "something else", t1.title 1269 assert_equal "something entirely new", t2.title 1270 end 1251 1271 1252 1272 def test_define_attr_method_with_value 1253 1273 k = Class.new( ActiveRecord::Base ) -
lib/active_record/base.rb
old new 569 569 update_counters(id, counter_name => -1) 570 570 end 571 571 572 def reload(instances, options={}) 573 return if instances.empty? 574 575 new_instances = find_all_by_id( instances.map(&:id) ) 576 instances.each_with_index do |instance, index| 577 instance.clear_aggregation_cache 578 instance.clear_association_cache 579 new_attrs = new_instances[index].instance_variable_get('@attributes') 580 instance.instance_variable_get( '@attributes' ).update( new_attrs ) 581 end 582 583 end 572 584 585 573 586 # Attributes named in this macro are protected from mass-assignment, such as <tt>new(attributes)</tt> and 574 587 # <tt>attributes=(attributes)</tt>. Their assignment will simply be ignored. Instead, you can use the direct writer 575 588 # methods to do assignment. This is meant to protect sensitive attributes from being overwritten by URL/form hackers. Example: … … 1715 1728 # may do e.g. record.reload(:lock => true) to reload the same record with 1716 1729 # an exclusive row lock. 1717 1730 def reload(options = nil) 1718 clear_aggregation_cache 1719 clear_association_cache 1720 @attributes.update(self.class.find(self.id, options).instance_variable_get('@attributes')) 1731 self.class.reload([self], options) 1721 1732 self 1722 1733 end 1723 1734