有这个:
class User < ActiveRecord::Base
after_save :execute_after_save
def execute_after_save
Kernel.puts "Actual object still not saved" if changed?
end
end
这Kernel.puts
句话应该被称为never因为对象被保存后它没有改变。
1.9.3p286 :003 > u = User.create!(:name => "Wadus Name")
Actual object still not saved
=> #<User id: 1, name: "Wadus Name">
1.9.3p286 :004 > u.changed?
=> false
1.9.3p286 :004 > u.name = "Other Name"
=> "Other Name"
1.9.3p286 :005 > u.changed?
=> true
1.9.3p286 :006 > u.save!
Actual object still not saved
=> true
1.9.3p286 :007 > u.changed?
=> false
查看所有Actual object still not saved
不应该出现的句子。
我期待在保存对象后after_save
实际调用回调。
这种情况让我疯狂,我必须做一些dirty objects
组合callbacks
。