diff options
author | José Valim <jose.valim@gmail.com> | 2011-10-14 09:42:03 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-10-14 09:42:03 -0700 |
commit | 521a089166a17d447c3b3b32ff7f9394774ca895 (patch) | |
tree | 4de9603d0f57beb70d7374bf46b073e3995731d9 /activerecord/lib | |
parent | 401d00d296c0c4dafd0e1103051f6adf0ae56fc5 (diff) | |
parent | c6f0461e898601578fa8160608d19fec319067fa (diff) | |
download | rails-521a089166a17d447c3b3b32ff7f9394774ca895.tar.gz rails-521a089166a17d447c3b3b32ff7f9394774ca895.tar.bz2 rails-521a089166a17d447c3b3b32ff7f9394774ca895.zip |
Merge pull request #3331 from fabrik42/master
Include affected record when raising an ActiveRecord::StaleObjectError
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/errors.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/locking/optimistic.rb | 4 |
2 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index 96870cb338..fc80f3081e 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -99,6 +99,16 @@ module ActiveRecord # # Read more about optimistic locking in ActiveRecord::Locking module RDoc. class StaleObjectError < ActiveRecordError + attr_reader :record, :attempted_action + + def initialize(record, attempted_action) + @record = record + @attempted_action = attempted_action + end + + def message + "Attempted to #{attempted_action} a stale object: #{record.class.name}" + end end # Raised when association is being configured improperly or diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index d9ad7e4132..2df3309648 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -103,7 +103,7 @@ module ActiveRecord affected_rows = connection.update stmt unless affected_rows == 1 - raise ActiveRecord::StaleObjectError, "Attempted to update a stale object: #{self.class.name}" + raise ActiveRecord::StaleObjectError.new(self, "update") end affected_rows @@ -127,7 +127,7 @@ module ActiveRecord affected_rows = self.class.unscoped.where(predicate).delete_all unless affected_rows == 1 - raise ActiveRecord::StaleObjectError, "Attempted to delete a stale object: #{self.class.name}" + raise ActiveRecord::StaleObjectError.new(self, "destroy") end end |