代码之家  ›  专栏  ›  技术社区  ›  JP Silvashy Gautam Rege

在Rails中正确加载ruby模块

  •  0
  • JP Silvashy Gautam Rege  · 技术社区  · 14 年前

    我不知道我做错了什么,我有一个文件在里面 lib/acts_as_votable.rb ,它只是一个应用程序的votable系统。

    module ActsAsVotable
    
    end
    
    module ActiveRecord
      class Base
        class << self
          cattr_accessor :votable
    
          def acts_as_votable
            has_many :votes, :as => :voteable
          end
    
          def votable?
            method_defined? :votes
          end
        end
    
        def votable?
          self.class.send(:method_defined?, :votes)
        end
      end
    end
    

    但似乎模块从未加载:

    undefined local variable or method `acts_as_votable' for #<Class:0x00000101796d80>
    

    加载模块的正确方法是什么?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Daniel O'Hara    14 年前

    config/initializers 目录,因此它们将由Rails自动预加载。

        2
  •  0
  •   robertwbradford    13 年前

    config.autoload_paths += %W(#{config.root}/lib/)