module ReloadAttribute::InstanceMethods

Methods added to all ActiveRecord models

Public Instance Methods

reload_attribute(*attrs) click to toggle source

Updates the models supplied attributes from the database. @param attrs [String] the attributes to reload @return [Hash] the attributes that were reloaded @return [nil] if the model is not persisted

# File lib/reload_attribute/instance_methods.rb, line 11
def reload_attribute(*attrs)
  return if new_record?
  values = self.class.where(id: id).select(attrs.join(', ')).first.slice(*attrs)
  values.each_with_object({}) do |attr, memo|
    name, value = attr
    self[name] = memo[name] = value
    memo
  end
end
Also aliased as: reload_attributes
reload_attributes(*attrs)
Alias for: reload_attribute