Ruby on rails Rails属于/有许多外部模型
在我的Rails 5.2应用程序中,我想引用另一个模型。我在应用程序中有以下设置:Ruby on rails Rails属于/有许多外部模型,ruby-on-rails,foreign-keys,ruby-on-rails-5,ruby-on-rails-5.2,Ruby On Rails,Foreign Keys,Ruby On Rails 5,Ruby On Rails 5.2,在我的Rails 5.2应用程序中,我想引用另一个模型。我在应用程序中有以下设置: class SomeModule::AnotherModule::User < ApplicationRecord has_many :phones end class Phone < ApplicationRecord belongs_to :user, optional: true, class_name: '::SomeModule::AnotherModule::User' end
class SomeModule::AnotherModule::User < ApplicationRecord
has_many :phones
end
class Phone < ApplicationRecord
belongs_to :user, optional: true, class_name: '::SomeModule::AnotherModule::User'
end
现在,当我尝试通过电话呼叫用户时,我得到以下信息:
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'
删除class\u name:
属性不会改变任何内容
我做错了什么?classsomemodule::AnotherModule::Userclass 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
有很多:手机,类名:'Phone',外键:'user\u id'
结束
类电话<应用记录
属于:user,可选:true,类名:'SomeModule::AnotherModule::user',外键:'user\u id'
结束
class SomeModule::AnotherModule::User
Try有很多:手机,类名:'Phone'
@JagdeepSingh没有任何功能difference@Severin使用外键:'user\u id'
,让我们试试下面给定的解决方案。try有很多:电话,类名:'Phone'
@JagdeepSingh不做任何设置difference@Severin使用外键:“用户id”,让我们试试下面给出的解决方案。
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