On Friday, January 19, 2018 at 8:47:29 AM UTC, 정인중 wrote:
> 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