Reputation: 8588
In my Rails 5.2 application I want to reference another Model. I have the following setup in the application:
class SomeModule::AnotherModule::User < ApplicationRecord
has_many :phones
end
class Phone < ApplicationRecord
belongs_to :user, optional: true, class_name: '::SomeModule::AnotherModule::User'
end
The migration was done like so:
add_reference :phones, :user, foreign_key: true, index: true
Now, when I try to call upon the user from a phone I get this:
Phone.first.user
#=> NameError: uninitialized constant User::Phone
from /home/testuser/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.0/lib/active_record/inheritance.rb:196:in `compute_type'
Removing the class_name:
attribute does not change anything.
What am I doing wrong?
Upvotes: 0
Views: 287
Reputation: 6531
class SomeModule::AnotherModule::User < ApplicationRecord
has_many :phones, class_name: 'Phone', foreign_key: 'user_id'
end
class Phone < ApplicationRecord
belongs_to :user, optional: true, class_name: 'SomeModule::AnotherModule::User', foreign_key: 'user_id'
end
Upvotes: 3