model 可以回调,其实关联也可以回调,只不过它的回调方法比较少:只有下面几个:
before_add
after_add
before_remove
after_remove
用法
class Author < ApplicationRecord
has_many :books, before_add: :validate_book_status
def validate_book_status
# 做book的验证
end
end
model 可以回调,其实关联也可以回调,只不过它的回调方法比较少:只有下面几个:
before_add
after_add
before_remove
after_remove
class Author < ApplicationRecord
has_many :books, before_add: :validate_book_status
def validate_book_status
# 做book的验证
end
end
本文标题:2018-08-08-关联回调
本文链接:https://www.haomeiwen.com/subject/okffbftx.html
网友评论