Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails属于/有许多外部模型_Ruby On Rails_Foreign Keys_Ruby On Rails 5_Ruby On Rails 5.2 - Fatal编程技术网

Ruby on rails Rails属于/有许多外部模型

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

在我的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
现在,当我尝试通过电话呼叫用户时,我得到以下信息:

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