开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜