7

I have 2 questions in rails app context:

I have some classes which aren't "modele", but require in my sytem, so I want separe them

1) How can I add "class' folder in app/? (if I create it and put classes, their are no included)

2) how can I put folder "model" in "app/class" folder (same thing here, the model are not included if I move it)

thx.

Matrix
  • 3,458
  • 6
  • 40
  • 76

2 Answers2

13

It´s kind of unclear what you are asking.

But if you want to autoload additional directories you can do it by placing something like this in config/application.rb

config.autoload_paths << Rails.root.join('app/class')

But please don´t call your directory class, use something descriptive instead.

By convention code that does not fit inside models, controllers, views, helpers or concerns and placed in the lib directory at the project root.


Edit:

You can load subdirectories by using a glob:

config.autoload_paths << Rails.root.join('app/classes/**/')
max
  • 96,212
  • 14
  • 104
  • 165
  • This solution don't works. It works only if I put file directly, but if I want organized my classes with sub-folder, they are not included (exemple: "app/classes/effects/attribute.rb" with "class Effect::Attribute"). Same thing if I move "model" folder in the new folder "classes", Models are not finded... – Matrix Oct 16 '14 at 14:29
  • ok, so by default, its not recursive on sub-folder... strange. so "**/" is the good solution, thx ! – Matrix Oct 16 '14 at 14:52
  • how can I control the order of inclusion? initializers for exemple are called before models, but if I include them with this solution to expend app_path, how can I define the order of inclusion files? – Matrix Oct 16 '14 at 15:36
  • I'd recommend to use the `lib/` folder only external code, like services and other decoupled modules/classes from your app. – vinibrsl Aug 20 '18 at 17:06
2

For quite some time Rails has autoloaded all paths under /app, as mentioned here

You may have run into a problem when using a "app/class" directory since "class" is a reserved word and "Class" is a class in Ruby.

There is a problem with your example:

exemple: "app/classes/effects/attribute.rb" with "class Effect::Attribute"

Notice that in the file path "effects" has an "s" at the end, whereas your module name does not "Effect::Atttribute". Those should match. Either both end with "s" or not, and when they do match Rails autoloading should work.

You should remove any of the other suggestions about appending to config.autoload_paths.

Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43