Today I decided to reorganize big amount of user related models and I'm having a problem with it.
Before I had such structure:
app/models/user.rb
app/models/user_info.rb
app/models/user_file.rb
...
So I moved all user_
models to user subfolder like this:
app/models/user.rb
app/models/user/info.rb
app/models/user/file.rb
...
and changed their definitions to
class User::Info < ActiveRecord::Base
class User::File < ActiveRecord::Base
...
User
model wasn't changed (except associations).
Everything works fine except User::File
model. When i'm trying to access this model i get the following error:
warning: toplevel constant File referenced by User::File
and indeed it returns standard ruby File class.
What i'm doing wrong?
UPD1:
root# rails c
Loading development environment (Rails 3.2.13)
2.0.0p195 :001 > User::File
(irb):1: warning: toplevel constant File referenced by User::File
=> File
2.0.0p195 :002 > User::Info
=> User::Info(...)
UPD2:
2.0.0p195 :001 > User::SomeModel
NameError: uninitialized constant User::SomeModel
2.0.0p195 :002 > User::IO
(irb):2: warning: toplevel constant IO referenced by User::IO
=> IO
2.0.0p195 :003 > User::Kernel
(irb):3: warning: toplevel constant Kernel referenced by User::Kernel
=> Kernel
My app doesn't have any IO or Kernel classes, except ruby default.
UPD3:
# app/models/user.rb
class User < ActiveRecord::Base
has_many :files, class_name: 'User::File'
..
end
# app/models/user/file.rb
class User::File < ActiveRecord::Base
belongs_to :user
# some validations, nothing serious
end
Update: This years Christmas present was the release of Ruby 2.5.0 with which this error won't happen anymore. With Ruby 2.5+ you will either get the constant you asked for or an error. For older Ruby versions read on:
Your User::File
class is not loaded. You have to require it (e.g. in user.rb
).
The following happens when ruby/rails sees User::Info
and evaluates it (simplified; only User
is defined yet).
User::Info
is defined - it is not (yet)Info
is defined - it is not (yet)uninitialized constant
-> do rails magic to find the user/info.rb
file and require itUser::Info
Now lets do it again for User::File
User::File
is defined - it is not (yet)File
is defined - it is (because ruby has a built in File
class)!User::File
but got ::File
::File
We observe that the rails magic, that automatically requires files for (yet) unknown constants, does not work for User::File
because File
is not unknown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With