How to write this method in separate module?
Right now I have this method in specific model.
self.reflect_on_all_associations(:has_many).each do |association|
define_method "#{association.name}?" do
self.send(association.name).any?
end
end
I 开发者_开发知识库want to use this method in every model. How can I write this method in separate module ?
Untested and from memory, but the following should work
module ReflectOnAssocations
def included(base)
base.reflect_on_all_associations(:has_many).each do |association|
define_method "#{association.name}?" do
self.send(association.name).any?
end
end
end
end
精彩评论