From f27a8eb71a14a9e12e26f182a3694a44abe81ee5 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Sat, 21 May 2011 13:14:55 -0300 Subject: New #update_columns method. --- activerecord/lib/active_record/persistence.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index c9e3fc5b05..f89b6b68d7 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -229,6 +229,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+. -- cgit v1.2.3 From a0b85b9ada9c5afbe891f290e9f2effbcb79aeab Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Thu, 26 Jul 2012 11:44:38 +1200 Subject: Suggest using update_columns instead of update_column. update_column is deprecated in Rails 4.0 so it makes no sense to recommend adopting it only to require changing to update_columns in the very next release. --- activerecord/lib/active_record/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index f89b6b68d7..d4c9e4f6cf 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -179,7 +179,7 @@ module ActiveRecord # def update_attribute(name, value) name = name.to_s - ActiveSupport::Deprecation.warn("update_attribute is deprecated and will be removed in Rails 4. If you want to skip mass-assignment protection, callbacks, and modifying updated_at, use update_column. If you do want those things, use update_attributes.") + ActiveSupport::Deprecation.warn("update_attribute is deprecated and will be removed in Rails 4. If you want to skip mass-assignment protection, callbacks, and modifying updated_at, use update_columns. If you do want those things, use update_attributes.") raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name) send("#{name}=", value) save(:validate => false) -- cgit v1.2.3