diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-25 22:54:55 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-25 23:02:30 -0300 |
commit | 50bdb924ba26999a468ec4844917cefec39ba08c (patch) | |
tree | b3e1ac7383b62cabe07234e911417f88033e8352 /activerecord/lib | |
parent | a1d18ed9cf24a8e12066ecb07fd702673ac68e63 (diff) | |
download | rails-50bdb924ba26999a468ec4844917cefec39ba08c.tar.gz rails-50bdb924ba26999a468ec4844917cefec39ba08c.tar.bz2 rails-50bdb924ba26999a468ec4844917cefec39ba08c.zip |
Revert "Remove update_attribute."
This reverts commit a7f4b0a1231bf3c65db2ad4066da78c3da5ffb01.
Conflicts:
activerecord/lib/active_record/associations/has_one_association.rb
activerecord/lib/active_record/persistence.rb
activerecord/test/cases/base_test.rb
activerecord/test/cases/dirty_test.rb
activerecord/test/cases/timestamp_test.rb
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 22 |
2 files changed, 20 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 703265c334..c1d57855a9 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -236,7 +236,7 @@ module ActiveRecord # add_column :people, :salary, :integer # Person.reset_column_information # Person.all.each do |p| - # p.update_column :salary, SalaryCalculator.compute(p) + # p.update_attribute :salary, SalaryCalculator.compute(p) # end # end # end @@ -256,7 +256,7 @@ module ActiveRecord # ... # say_with_time "Updating salaries..." do # Person.all.each do |p| - # p.update_column :salary, SalaryCalculator.compute(p) + # p.update_attribute :salary, SalaryCalculator.compute(p) # end # end # ... diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 6b4b9bd103..c64e3e6ccf 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -1,4 +1,3 @@ - module ActiveRecord # = Active Record Persistence module Persistence @@ -166,6 +165,21 @@ module ActiveRecord became end + # Updates a single attribute and saves the record. + # This is especially useful for boolean flags on existing records. Also note that + # + # * Validation is skipped. + # * Callbacks are invoked. + # * updated_at/updated_on column is updated if that column is available. + # * Updates all the attributes that are dirty in this object. + # + def update_attribute(name, value) + name = name.to_s + verify_readonly_attribute(name) + send("#{name}=", value) + save(:validate => false) + 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. @@ -242,7 +256,7 @@ module ActiveRecord # Saving is not subjected to validation checks. Returns +true+ if the # record could be saved. def increment!(attribute, by = 1) - increment(attribute, by).update_columns(attribute => self[attribute]) + increment(attribute, by).update_attribute(attribute, self[attribute]) end # Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1). @@ -259,7 +273,7 @@ module ActiveRecord # Saving is not subjected to validation checks. Returns +true+ if the # record could be saved. def decrement!(attribute, by = 1) - decrement(attribute, by).update_columns(attribute => self[attribute]) + decrement(attribute, by).update_attribute(attribute, self[attribute]) end # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So @@ -276,7 +290,7 @@ module ActiveRecord # Saving is not subjected to validation checks. Returns +true+ if the # record could be saved. def toggle!(attribute) - toggle(attribute).update_columns(attribute => self[attribute]) + toggle(attribute).update_attribute(attribute, self[attribute]) end # Reloads the attributes of this object from the database. |