Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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/5/ruby/22.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/3/reactjs/24.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_Ruby_Database_Inheritance - Fatal编程技术网

Ruby on rails Rails继承与关系问题

Ruby on rails Rails继承与关系问题,ruby-on-rails,ruby,database,inheritance,Ruby On Rails,Ruby,Database,Inheritance,我在项目中使用单表继承。我将给出以下代码,而不是详细解释: # person_profile.rb class PersonProfile < ActiveRecord::Base belongs_to :Person end # company_profile.rb class CompanyProfile < ActiveRecord::Base belongs_to :Company end # person.rb class Person < User

我在项目中使用单表继承。我将给出以下代码,而不是详细解释:

# person_profile.rb
class PersonProfile < ActiveRecord::Base
  belongs_to :Person
end

# company_profile.rb
class CompanyProfile < ActiveRecord::Base
  belongs_to :Company
end

# person.rb
class Person < User
  has_one :PersonProfile
end

# company.rb
class Company < User
  has_one :CompanyProfile
end

Rails正在表person\u profiles中查找person\u id,但该表中只有一个user\u id。修复此错误的最佳方法是什么?

模型关联中指定的模型应为小写,每个单词之间用下划线分隔。因此:

class PersonProfile < ActiveRecord::Base
  belongs_to :person
end

class CompanyProfile < ActiveRecord::Base
  belongs_to :company
end

class Person < User
  has_one :person_profile
end

class Company < User
  has_one :company_profile
end
class PersonProfile
模型关联中指定的模型应为小写,每个单词之间用下划线分隔。因此:

class PersonProfile < ActiveRecord::Base
  belongs_to :person
end

class CompanyProfile < ActiveRecord::Base
  belongs_to :company
end

class Person < User
  has_one :person_profile
end

class Company < User
  has_one :company_profile
end
class PersonProfile
我必须将外键指定为“user\u id”,因为它默认认为是“person\u id”

class Person < User
   has_one :person_profile, :foreign_key => 'user_id'
end

class Company < User
   has_one :company_profile, :foreign_key => 'user_id'
end
class-Person“user\u id”
结束
类公司<用户
有一个:公司档案,:外键=>'user\u id'
结束

我必须将外键指定为“user\u id”,因为它默认认为是“person\u id”

class Person < User
   has_one :person_profile, :foreign_key => 'user_id'
end

class Company < User
   has_one :company_profile, :foreign_key => 'user_id'
end
class-Person“user\u id”
结束
类公司<用户
有一个:公司档案,:外键=>'user\u id'
结束

您可以使用
:foreign\u key
选项has\u one
来指定键

例如:

has_one :person, :foreign_key => "user_id"

请参阅参考。

您可以使用
:foreign\u key
选项has\u one
来指定键

例如:

has_one :person, :foreign_key => "user_id"

参见参考资料。

没错,那么我想我会说
@person.person\u profile==nil
。但这仍然不能解决问题。我得到了相同的mysql错误。这是真的,那么我想我会说
@person.person\u profile==nil
。但这仍然不能解决问题。我得到同样的mysql错误。