inverse_of 似乎在 after_initialize 回调中不起作用
class User < ActiveRecord::Base
has_many :faces, :inverse_of => :user
end
class Face < ActiveRecord::Base
belongs_to :user, :inverse_of => :faces
after_initialize :init
def init
p user.object_id
end
end
u = User.find(56)
u.object_id
=> 70242500754120
u.faces.first.user.object_id
Face Load (0.3ms) SELECT `faces`.* FROM `faces` WHERE `faces`.`user_id` = 56 LIMIT 1
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 56 LIMIT 1
70242477010060
=> 70242500754120
u.faces.first.user == u
=> true
回调内部有不同的对象,但外部是相同的。看起来像在设置 inverse_of 魔法之前调用的回调。
任何解决方法的建议,允许在 after_initialize 回调中访问相同的对象?谢谢