12

I organized some of my rails models in folders which I am autoloading with

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]

I can use all models directly(e.g Image.first.file_name) but when I try to access them through relationships, e.g. @housing.images.each do... with has_many: images I get the following error

Unable to autoload constant Housing::HousingImage, expected /path/app/models/housing/image.rb to define it

How do i get rails to use my models for the relationship methods?

I'm running ruby 2.2 and rails 4.2

1 Answer 1

20

Rails automatically loads models from subfolders but does expect them to have namespace.

/app/models/user.rb
class User
end

/app/models/something/user.rb
class Something::User
end

If you do not properly namespace your models in subfolders it will mess up Rails autoloader and cause errors like you see.

Remove this

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]

And add the proper namespaces to your models and everything will work fine.

You can easily use namespaced models in your relationships like this:

class User
  has_many :photos, class_name: 'Something::Photo'
end

user.photos (will be instances of Something::Photo)

If you do not want to use the namespacing but split up your models for other reason, you can do that at the top-level and use other folders next to models. By default rails loads all the folders in apps, so you could just make a folder "models2" or whatever you want to call it next to "models". This will not have any effect on the functionality of the rails class loading.

Given your example you could then do:

/app
  /controllers
  /models
    for all your normal models
  /housing
    for your "housing" models

Like this you can directly access them at the top level namespace, no class_name settings or anything needed.

6
  • 2
    Correct, but I dont wont them namespaced, wihich is why i added the config in the first place. I also tried the class_name option with the non-namespaced class name but it didn't work either. Feb 19, 2015 at 17:16
  • 1
    Within the models (or any other rails folder) you need to stick to the rails naming convention otherwise this will cause you a lot of headache. Alternatively you could split up the models folder directly (see my updated answer in a moment)
    – maxigs
    Feb 20, 2015 at 17:34
  • Ok, I will go with your solution, but I think the class_name option on the relationship should probably work? I will open a issue on github to investigate further. Feb 21, 2015 at 14:57
  • @ReneKoller has the github inquiry provided any proper solution? I'm facing the exact same problem now.
    – Spätzle
    Jul 5, 2016 at 11:20
  • 1
    @JenuelGanawed I would recommend it, yes. But it depends on the structure of your project at the end which is the best approach
    – maxigs
    Feb 2, 2020 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.