Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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)

patch which adds ActiveRecord::Base.reload

  • test/base_test.rb

    old new  
    12481248    t2.reload 
    12491249    assert_equal t1.title, t2.title 
    12501250  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 
    12511271 
    12521272  def test_define_attr_method_with_value 
    12531273    k = Class.new( ActiveRecord::Base ) 
  • lib/active_record/base.rb

    old new  
    569569        update_counters(id, counter_name => -1) 
    570570      end 
    571571 
     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 
    572584 
     585 
    573586      # Attributes named in this macro are protected from mass-assignment, such as <tt>new(attributes)</tt> and 
    574587      # <tt>attributes=(attributes)</tt>. Their assignment will simply be ignored. Instead, you can use the direct writer 
    575588      # methods to do assignment. This is meant to protect sensitive attributes from being overwritten by URL/form hackers. Example: 
     
    17151728      # may do e.g. record.reload(:lock => true) to reload the same record with 
    17161729      # an exclusive row lock. 
    17171730      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) 
    17211732        self 
    17221733      end 
    17231734