vendor/bundle/gems/activerecord-4.0.0/lib/active_record/persistence.rb
# Reloads the record from the database.
#
# This method modifies the receiver in-place. Attributes are updated, and
# caches busted, in particular the associations cache.
#
# If the record no longer exists in the database <tt>ActiveRecord::RecordNotFound</tt>
# is raised. Otherwise, in addition to the in-place modification the method
# returns +self+ for convenience.
#
# The optional <tt>:lock</tt> flag option allows you to lock the reloaded record:
#
#   reload(lock: true) # reload with pessimistic locking
#
# Reloading is commonly used in test suites to test something is actually
# written to the database, or when some action modifies the corresponding
# row in the database but not the object in memory:
#
#   assert account.deposit!(25)
#   assert_equal 25, account.credit        # check it is updated in memory
#   assert_equal 25, account.reload.credit # check it is also persisted
#
# Another commom use case is optimistic locking handling:
#
#   def with_optimistic_retry
#     begin
#       yield
#     rescue ActiveRecord::StaleObjectError
#       begin
#         # Reload lock_version in particular.
#         reload
#       rescue ActiveRecord::RecordNotFound
#         # If the record is gone there is nothing to do.
#       else
#         retry
#       end
#     end
#   end
#
Reloads the record from the database.
This method modifies the receiver in-place. Attributes are updated, and
caches busted, in particular the associations cache.

If the record no longer exists in the database ActiveRecord::RecordNotFound
is raised. Otherwise, in addition to the in-place modification the method
returns +self+ for convenience.

The optional :lock flag option allows you to lock the reloaded record.

true) # reload with pessimistic locking

Reloading is commonly used in test suites to test something is actually
written to the database, or when some action modifies the corresponding
row in the database but not the object in memory.

assert account.deposit!(25)
assert_equal 25, account.credit        # check it is updated in memory
assert_equal 25, account.reload.credit # check it is also persisted

Another commom use case is optimistic locking handling.


Reloads the record from the database . This method modifies the receiver in - place . Attributes are updated and caches busted in particular the associations cache . If the record no longer exists in the database ActiveRecord :: RecordNotFound is raised . Otherwise in addition to the in - place modification the method returns + self + for convenience . The optional : lock flag option allows you to lock the reloaded record . true ) # reload with pessimistic locking Reloading is commonly used in test suites to test something is actually written to the database or when some action modifies the corresponding row in the database but not the object in memory . assert account . deposit! ( 25 ) assert_equal 25 account . credit # check it is updated in memory assert_equal 25 account . reload . credit # check it is also persisted Another commom use case is optimistic locking handling .
def reload(options = nil)
      clear_aggregation_cache
      clear_association_cache
      fresh_object =
        if options && options[:lock]
          self.class.unscoped { self.class.lock.find(id) }
        else
          self.class.unscoped { self.class.find(id) }
        end
      @attributes.update(fresh_object.instance_variable_get('@attributes'))
      @columns_hash = fresh_object.instance_variable_get('@columns_hash')
      @attributes_cache = {}
      self
    end
def reload ( options = nil ) clear_aggregation_cache clear_association_cache fresh_object = if options && options [ :lock ] self . class . unscoped { self . class . lock . find ( id ) } else self . class . unscoped { self . class . find ( id ) } end @attributes . update ( fresh_object . instance_variable_get ( '@attributes' ) ) @columns_hash = fresh_object . instance_variable_get ( '@columns_hash' ) @attributes_cache = { } self end
Reloads the record from the database.
Reloads the record from the database .
options
<not_specific>