Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 3.2 我们的Rails建模思维有什么问题?_Ruby On Rails 3.2_Associations_Single Table Inheritance - Fatal编程技术网

Ruby on rails 3.2 我们的Rails建模思维有什么问题?

Ruby on rails 3.2 我们的Rails建模思维有什么问题?,ruby-on-rails-3.2,associations,single-table-inheritance,Ruby On Rails 3.2,Associations,Single Table Inheritance,如果用户-->学生//员工(单表继承),并且他们都属于组织-->学校//工作(单表继承),那么编写关联的正确方式是什么?我将organization_id放入User类中,并在相应的子类中编写了属于/拥有多个,但是当我调用User.school时,我得到了“nil”,即使他有一个organization_id=1 user.rb class User < ActiveRecord::Base attr_accessible :email, :name, :password, :orga

如果用户-->学生//员工(单表继承),并且他们都属于组织-->学校//工作(单表继承),那么编写关联的正确方式是什么?我将organization_id放入User类中,并在相应的子类中编写了属于/拥有多个,但是当我调用User.school时,我得到了“nil”,即使他有一个organization_id=1

user.rb

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :organization_id, :type
end
class用户
student.rb

class Student < User
  belongs_to :school
end
class学生
employee.rb

class Employee < User
  belongs_to :company
end
class-Employee
organization.rb

class Organization < ActiveRecord::Base
  attr_accessible :name
end
类组织
school.rb

class School < Organization
  has_many :students
end
班级学校<组织机构
你有很多学生吗
结束
company.rb

class Company < Organization
  has_many :employees
end
class公司
我强烈建议您不要使用单表继承。你从中得到了什么好处?学生和员工是不同的,学校和公司也是不同的。它们应该是分开的桌子。从长远来看,这会让你的生活变得如此简单。如果不使用单表继承,这个问题也会消失。

用户类不定义学校关联,对学生类一无所知。所以你不能指望User.school工作。您可以期望Student.school工作,因为这是您的代码定义的

STI从列的角度来看,它只是添加了一个类型字符串列,显示记录映射到的模型的子类型

您还可以重新考虑如何为数据建模。学生属于一所学校,但用户也可能属于一家公司或两所学校