我想在我的Rails应用程序中的每个ActiveRecord模型中包含此模块,而不会在每个文件中删除include NotificationResourceableTraits
。有办法吗?
module NotificationResourceableTraits
def self.included(base)
base.has_many :notification_resources, :as => :notification_resourceable
base.has_many :notifications, :through => :notification_resources
end
end
答案 0 :(得分:1)
我刚刚解决了这个问题。看看我的要点:http://gist.github.com/404298
你可以这样做:
ModelDiscovery.valid_active_record_classes.each do |model|
model.include YourMixinModule
end