该关联是has_and_belongs_to_many
并且在 gem 中定义。我需要知道什么时候添加了一个新的,所以我可以告诉答案是after_add
回调,但我不知道事后如何添加它。
我现在拥有的代码不起作用:
(在宝石中)
工作.rb
module Spree
class Job < Spree::Base
has_and_belongs_to_many :users, join_table: 'spree_jobs_users', class_name: Spree.user_class.to_s
end
end
(我的破代码)
job_decorator.rb
Spree::Job.class_eval do
has_and_belongs_to_many :users, join_table: 'spree_jobs_users',
class_name: Spree.user_class.to_s, after_add: :test
def test
# after method
end
end
有什么办法可以做到这一点吗?或者有什么其他方法可以让我知道何时添加了新工作?