lun*_*unr 3 activerecord ruby-on-rails rails-activerecord
我有两个班级和一个belongs_to协会:
class User < ActiveRecord::Base
belongs_to :foo
before_update do |user|
self.foo = Foo.create
self.foo.save
end
end
class Foo < ActiveRecord::Base
after_update do |foo|
puts "after update is called"
end
end
Run Code Online (Sandbox Code Playgroud)
当用户更新时,我创建一个foo并保存它.但是当我这样做的时候,正在调用它的after_update回调,Foo据我所知,只有当记录更新未被创建时才调用.我究竟做错了什么?
Foo#after_update正在调用因为你save在创建它之后调用了foo.所以你创建foo然后更新它.删除呼叫self.foo.save
before_update do |user|
self.foo = Foo.create # this creates foo
self.foo.save # this updates foo
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1061 次 |
| 最近记录: |