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错误。