简体   繁体   English

如何在Rails 4中包含lib文件夹中的类

[英]How do I include a class from lib folder in Rails 4

I have this class in my libs folder 我的libs文件夹中有此类

class AboutMe
    include HTTParty
    base_uri 'api.about.me'



    def initialize(email)
        @options = { :query => {:email => service, :extended => true } }
    end

    def search
      self.class.get("/api/v2/json/users/search", @options)
    end
end

And I have a model 我有一个模特

class Record < ActiveRecord::Base
  def some_method
  end
end

How do I include that class to be used in my model? 如何在模型中包含要使用的类?

All you need to do is require the particular class in your model. 您需要做的就是在模型中需要特定的类。

For example: Add the following line at the top of your model Record 例如:在模型顶部添加以下行Record

require 'about_me'

Alternatively, you can autoload lib/ folder including all subdirectories in your application.rb 或者,您可以自动加载lib/文件夹,包括application.rb所有子目录。

config.autoload_paths += Dir["#{config.root}/lib/**/"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM