How does Rails find models and controllers? How can I download it to load more models?

I am trying to create a non-ActiveRecord's model app/models/gamestate.rb. Then inside my controller (PlayController) I would have to do GameState.new, right? No:

NameError (uninitialized constant PlayController::GameState):
  app/controllers/play_controller.rb:23:in `play'

(at least in the development environment)

But! If I have a model called app/models/play.rb, it automatically loads, and I can do it Play.new.

So my question is: how does Rails know which classes to load? What is the name mangling does to get on play#actionto PlayControllerup app/controllers/play_controller.rbto app/models/play.rb?

It seems terribly fragile, but it might be better to understand how it works.

And finally, how can I download it for download app/models/gamestate.rb?

+3
3

Ryan, Rails. Rails 3, activesupport/lib/dependencies.rb load_missing_constant, GameState, , . "GameState", "" , game_state, autoload_paths. ( .rb), .

game_state.rb (, autoload_path), GameState , . .

, -AR- ; , config/application.rb, :

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

GameState lib/game_state.rb, Rails .

, Rails, - Ruby , , , ""; where caller (0) Rails. ( ), , , .

+5
, , . Rails app autoload_path. , Rails , , - , gamestate.rb game_state.rb - 't , .
+8

- gamestate.rb, Gamestate GameState ( Game_state.rb → GameState). Rails. , . .

Ruby/Rails: http://itsignals.cascadia.com.au/?p=7

+5
source

Source: https://habr.com/ru/post/1782154/


All Articles