0

I'm having this problem about accessing a Model in a subfolder. I have the following file structure in my project:

app/models
   - accounts
     - type1.rb #Inherits from Account
     - type2.rb #Inherits from Account
     - etc.   
   - account.rb
   - user.rb
   - etc.

Now in user.rb I have a function that tries to create accounts of type1 or type2:

def function
   self.account = ::Type1.new(...)
end

knowing that I added to my application.rb (following http://blog.hasmanythrough.com/2008/5/6/a-simple-alternative-to-namespaced-models) the following line:

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

so that the model subfolders are indeed loaded.

Now, I still get the famous uninitialized constant Type1 error message when I call up the function. What am I missing?

UPDATE:

The Type1 class is empty:

class Type1 < Account
end

and the Account class is straightforward:

class Account < ActiveRecord::Base

#======================RELATIONS======================
  belongs_to :currency
  belongs_to :organization

#======================VALIDATIONS=========================
  validates :name, :presence => true
  validates :country, :presence => true
  validates :currency, :presence => true
  validates :organization, :presence => true
end
muichkine
  • 2,890
  • 2
  • 26
  • 36

2 Answers2

3

Try this

config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
Abhay Kumar
  • 1,582
  • 1
  • 19
  • 45
  • Nope, I tried that already. I also checked that the folder was added to the path by dumping the config.autoload_paths variable: `C:/Users/.../lib C:/Users/.../app/models/ C:/Users/.../app/models/accounts/` – muichkine Sep 25 '12 at 09:18
  • Do u have a namespace to which type1 belongs ? – Abhay Kumar Sep 25 '12 at 09:24
  • Nope, I removed it: `class Type1 < Account` – muichkine Sep 25 '12 at 10:14
  • so account is just a folder name then you must try this code -- config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |f| File.stat(f).directory? } – Abhay Kumar Sep 25 '12 at 10:24
  • The folder is already added properly but I tested your code anyhow (just to see) and it yields the same list of paths. So problem is still here. I guess the problem does not come from the missing path in the config.autoload_paths. – muichkine Sep 25 '12 at 11:13
  • If u have used the above code u can just try Type1.new(...) instead of ::Type1.new(...). Since you dont have a namespace Type1.new(...) should work. – Abhay Kumar Sep 25 '12 at 11:32
  • But the whole mystery is that it does not :) If I do Type1.new() I get `uninitialized constant User::Type1` error. – muichkine Sep 25 '12 at 11:39
  • Just added them in the post's body for you. – muichkine Sep 25 '12 at 12:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17121/discussion-between-abhay-kumar-and-user1563325) – Abhay Kumar Sep 25 '12 at 12:12
0

OK, I finally found the thing. The idea came from this page: Rails 3 library not loading until require

My problem was actually the file name wat not respecting the CamelCase name of the class...

Community
  • 1
  • 1
muichkine
  • 2,890
  • 2
  • 26
  • 36