Unable to autoload constant issue

I am switching old systems created with php to Rails.

I want to use the table ‘servicename_user’ of the previous system in the User model.

There is no problem with the console or controller, but when I try to create the rspec test, I get the following error message:

Failure/Error: it { should validate_presence_of(:email) }

LoadError:

Unable to autoload constant USER, expected /Users/injung/Github/api/app/models/user.rb to define it

``

However, my user.rb file is located exactly where the error message was mentioned.

For reference, the test was written as follows.

require_relative ‘…/…/app/models/user’

describe User, type: :model do

context ‘validation’ do

it { should validate_presence_of(:author) }

it { should validate_presence_of(:email) }

end

end

``

The user model is as follows.

class User < ApplicationRecord

self.table_name = ‘servicename_user’

end

``

After some search, I found out that If my table name is servicename_user, my user model should be in app/models/servicename/user, but I do not want to do like this. Is there any good way?

It is difficult to change the table name because it references a lot on older systems.

 Failure/Error: it \{ should validate\_presence\_of\(:email\) \}

 LoadError:
   Unable to autoload constant USER, expected /Users/injung/Github/api/app/models/user\.rb to define it

Are there any references to USER (all caps) anywhere in your app? The next one of the backtrack would be a good place to look. When playing around in the console rails only loads files that it needs to load, whereas if my memory is correct in tests it loads everything upfront. In particular if you have (for example) a controller that you don’t use, maybe an experiment that didn’t work out that references USER, then this wouldn’t be triggered in develipment unless you accessed that controller but would be in tests.

However, my user.rb file is located exactly where the error message was mentioned.
For reference, the test was written as follows.

require_relative '../../app/models/user'

describe User, type: :model do
context 'validation' do
it { should validate_presence_of(:author) }
it { should validate_presence_of(:email) }
end
end

The user model is as follows.

class User < ApplicationRecord
self.table_name = 'servicename_user'
end

After some search, I found out that If my table name is servicename_user, my user model should be in app/models/servicename/user, but I do not want to do like this. Is there any good way?
It is difficult to change the table name because it references a lot on older systems.

The table name shouldn’t matter as far as constant lookup is concerned and looks like you’re setting it correctly.

Fred

Did you use the self.table_name = 'servicename_user' assignment in your User class? That's a critical part of getting a legacy database to work with Rails.

Walter

Thank you for reply :slight_smile:
I have used byebug to find out backtrace and realized that it was STI related issue. The servicename_user table was using type column.

Problem solved by setting

self.inheritance_column = :_type_disabled

``

2018년 1월 19일 금요일 오후 6시 1분 36초 UTC+9, Frederick Cheung 님의 말: