Cannot include module in model

前端 未结 3 969
清酒与你
清酒与你 2020-12-28 20:13

I\'m using

Ruby version              1.8.7
Rails version             3.0.3

I have a method called alive in every model of my rails app:

3条回答
  •  甜味超标
    2020-12-28 20:30

    include will treat those methods as instance methods, not class methods. What you want to do is this:

    module LifeControl    
      module ClassMethods
        def alive
          where('deleter is null')  
        end   
    
        def dead
          where('deleter is not null')  
        end    
      end
    
      def self.included(receiver)
        receiver.extend ClassMethods
      end
    end
    

    This way, alive and dead will be available on the class itself, not instances thereof.

提交回复
热议问题