What folders does Rails 4.1 include in autoload

2019-09-02 05:36发布

问题:

I am writing a Ruby on Rails 4.1 app with Ruby 2.11. The structure is Domain -> Team -> User. So, a user belongs to a team and I sometimes need to make a team from a domain (so they are associated). Think Starbucks -> NY Central Team -> Mr Barista.

I have made some builder classes and put them in app/builders, but when I try to use the class it says uninitialized constant in the Rails console. So, for example, I have the file in app/builders/team_builder.rb:

class TeamBuilder

  attr_reader :domain, :params

  def initialize(domain, params = {})
    @domain = domain
    @params = params
  end

  def build
    domain.teams.build(params)
  end

end

But when I type TeamBuilder.new(domain, name: 'Team name here!') I get the response

NameError: uninitialized constant TeamBuilder

It seems like it does not recognise the new class I have added above, which makes me think it is not loading it. But I thought that all sub-directories in app/ were loaded.

Totally stumped on this one, and I cannot find a guide or documentation on this (maybe it's there - somewhere...)

回答1:

Go into your rails console (rails c) and type the following:

YourAppName::Application.config.eager_load_paths

(where YourAppName is whatever it is called in your environment.rb where initialize! is called). This should show you all the paths automatically loaded into your application (before customizing).

It looks like the automatic path adding magic is happening in here https://github.com/rails/rails/blob/master/railties/lib/rails/engine/configuration.rb



回答2:

The docs indicate you can run:

bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'

to view in terminal