简体   繁体   中英

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

I have this class in my libs folder

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

require 'about_me'

Alternatively, you can autoload lib/ folder including all subdirectories in your application.rb

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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