Jan'uary » 日志 » save and save!
save and save!
Jan 发表于 2008-03-29 21:41:01
Maybe you already know the diff between save and save!. "Save doesn't throw exceptions but its bang! version does when save failed."
Is it true?
Partly, only when u don't use locks. If you have a lock_version column in table, rails will enable lock automatically and in this case, ActiveRecord#save may throw StaleObjectError.
class AddLockToUser < ActiveRecord:Migration
def self.up
add_column :users, :lock_version, :integer, :default => 0
end
#......
end
# somewhere in your controller
user1 = User.find 1
user2 = User.find 2
user1.boss = 'Bill Gates'
user2.boss = 'Rex'
user1.save
user2.save # here a StaleObjectError will be throw
Is it true?
Partly, only when u don't use locks. If you have a lock_version column in table, rails will enable lock automatically and in this case, ActiveRecord#save may throw StaleObjectError.
class AddLockToUser < ActiveRecord:Migration
def self.up
add_column :users, :lock_version, :integer, :default => 0
end
#......
end
# somewhere in your controller
user1 = User.find 1
user2 = User.find 2
user1.boss = 'Bill Gates'
user2.boss = 'Rex'
user1.save
user2.save # here a StaleObjectError will be throw
相关日志:
收藏:
QQ书签
del.icio.us
订阅:
Google
抓虾
