如何覆盖activerecord has_many关联构建方法?

时间:2013-05-02 14:53:30

标签: ruby-on-rails activerecord associations

我想覆盖has_many assoication的构建方法。

class OtherThings < ActiveRecord::Base
  belongs_to :my_model
end

class MyModel < ActiveRecord::Base
  has_many :other_things
end

这样

m = MyModel.create
m.other_things.build  # i'd like to add additional logic within the build method...

我已经通过更新OtherThings的新方法,但这有点混乱。我希望我的逻辑只有在与构建相关联时才会发生...

2 个答案:

答案 0 :(得分:0)

我不确定您尝试使用构建做什么。如果您尝试添加更多方法,只需将.whatever_method附加到最后。此外,模型将始终关联。

答案 1 :(得分:0)

我能够通过使用around_create来解决我的问题。

相关问题