diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-07-24 19:13:18 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-07-24 20:15:48 -0300 |
commit | da84ccb6f04080d0f18f9bce35f773f193b1770b (patch) | |
tree | f40aa33b4f2eca60d43a46ef9e9c9616e3b58791 /activerecord/lib/active_record | |
parent | 87ffc3662fc023d33e3baaf2fadb09ce4bece5b5 (diff) | |
download | rails-da84ccb6f04080d0f18f9bce35f773f193b1770b.tar.gz rails-da84ccb6f04080d0f18f9bce35f773f193b1770b.tar.bz2 rails-da84ccb6f04080d0f18f9bce35f773f193b1770b.zip |
Use update_columns to implemente the update_column
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index bd5d8de69c..e34dc11606 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -167,22 +167,6 @@ module ActiveRecord became end - # Updates a single attribute of an object, 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 the +name+ - # attribute is marked as readonly. - def update_column(name, value) - name = name.to_s - verify_readonly_attribute(name) - raise ActiveRecordError, "can not update on a new record object" unless persisted? - raw_write_attribute(name, value) - self.class.where(self.class.primary_key => id).update_all(name => value) == 1 - end - # Updates the attributes of the model from the passed-in hash and saves the # record, all wrapped in a transaction. If the object is invalid, the saving # will fail and false will be returned. @@ -211,6 +195,18 @@ module ActiveRecord end end + # Updates a single attribute of an object, 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 the +name+ + # attribute is marked as readonly. + def update_column(name, value) + update_columns(name => value) + end + # Updates the attributes from the passed-in hash, without calling save. # # * Validation is skipped. @@ -221,10 +217,15 @@ module ActiveRecord # 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_key do |key| + raise ActiveRecordError, "#{key.to_s} is marked as readonly" if self.class.readonly_attributes.include?(key.to_s) + end + attributes.each do |k,v| raw_write_attribute(k,v) end + self.class.where(self.class.primary_key => id).update_all(attributes) == 1 end |