【发布时间】:2011-03-11 02:34:03
【问题描述】:
有没有办法在将项目添加到 habtm 关系时添加回调?
比如我有以下两个模型,User和Role:
# user.rb
class User; has_and_belongs_to_many :roles; end
# role.rb
class Role; has_and_belongs_to_many :users; end
我想为<< 方法(@user << @role)添加回调,但我似乎找不到 ActiveRecord 回调,因为连接表没有模型(因为它是一个真正的习惯)。
我知道我可以编写像add_to_role(role) 这样的方法,并在其中定义所有内容,但我更喜欢使用回调。这可能吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 has-and-belongs-to-many