aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/persistence.rb
diff options
context:
space:
mode:
authorSebastian Martinez <sebastian@wyeworks.com>2011-05-21 13:14:55 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-07-24 19:06:22 -0300
commit87ffc3662fc023d33e3baaf2fadb09ce4bece5b5 (patch)
tree7f9c570f93ba30a78634648765159b636f5942ab /activerecord/lib/active_record/persistence.rb
parent2d6af1d387970d033ccf660a77305f2563fd36ef (diff)
downloadrails-87ffc3662fc023d33e3baaf2fadb09ce4bece5b5.tar.gz
rails-87ffc3662fc023d33e3baaf2fadb09ce4bece5b5.tar.bz2
rails-87ffc3662fc023d33e3baaf2fadb09ce4bece5b5.zip
New #update_columns method.
Diffstat (limited to 'activerecord/lib/active_record/persistence.rb')
-rw-r--r--activerecord/lib/active_record/persistence.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index a23597be28..bd5d8de69c 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -211,6 +211,23 @@ module ActiveRecord
end
end
+ # Updates the attributes from the passed-in hash, without calling save.
+ #
+ # * Validation is skipped.
+ # * Callbacks are skipped.
+ # * updated_at/updated_on column is not updated if that column is available.
+ #
+ # Raises an +ActiveRecordError+ when called on new objects, or when at least
+ # one of the attributes is marked as readonly.
+ def update_columns(attributes)
+ raise ActiveRecordError, "can not update on a new record object" unless persisted?
+ attributes.each_key {|key| raise ActiveRecordError, "#{key.to_s} is marked as readonly" if self.class.readonly_attributes.include?(key.to_s) }
+ attributes.each do |k,v|
+ raw_write_attribute(k,v)
+ end
+ self.class.where(self.class.primary_key => id).update_all(attributes) == 1
+ end
+
# Initializes +attribute+ to zero if +nil+ and adds the value passed as +by+ (default is 1).
# The increment is performed directly on the underlying attribute, no setter is invoked.
# Only makes sense for number-based attributes. Returns +self+.