Ruby on rails 如何在RubyonRails中设置关联?
我正在构建一个用于实践的示例应用程序,但很难确定组织模型和关联的最佳方式。让我们假设我有3个模型:Ruby on rails 如何在RubyonRails中设置关联?,ruby-on-rails,ruby,associations,models,Ruby On Rails,Ruby,Associations,Models,我正在构建一个用于实践的示例应用程序,但很难确定组织模型和关联的最佳方式。让我们假设我有3个模型: 学校 班级 学生 我想: 学校将有许多班级 班里有很多学生 属于学校的班级 学生将在许多不同学校的许多班级注册 这些联想让我头晕目眩,我不知道该用哪一种。非常感谢您的帮助 您的模型应如下所示: class School < ActiveRecord::Base has_many :classes has_many :students, :through => :classe
- 学校将有许多班级
- 班里有很多学生
- 属于学校的班级
- 学生将在许多不同学校的许多班级注册
这些联想让我头晕目眩,我不知道该用哪一种。非常感谢您的帮助 您的模型应如下所示:
class School < ActiveRecord::Base
has_many :classes
has_many :students, :through => :classes
end
class Class < ActiveRecord::Base
belongs_to :school
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :classes
end
班级学校:班
结束
类
确保学生表和班级表分别有Class\u id
和school\u id
列
此外,类是一个,因此可能会导致问题(您可能必须使用其他名称)您的模型应如下所示:
class School < ActiveRecord::Base
has_many :classes
has_many :students, :through => :classes
end
class Class < ActiveRecord::Base
belongs_to :school
has_and_belongs_to_many :students
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :classes
end
班级学校:班
结束
类
确保学生表和班级表分别有Class\u id
和school\u id
列
另外,类是一个类,所以它可能会导致问题(你可能必须使用不同的名称)尽管乍一看似乎学生应该直接属于类,但类并不是真正的“has\u and\u belies\u to \u many”替代物。为此,我将使用“注册”。(注意,在rails 3.1中,您现在可以执行嵌套:直通调用。) 这里有一个比前一个评论者的实现稍微高级一些的实现:
class School << ActiveRecord::Base
has_many :academic_classes
has_many :enrollments, :through => :academic_classes
has_many :students, :through => :enrollments, :uniq => true
end
class AcademicClass << ActiveRecord::Base
belongs_to :school
has_many :enrollments
end
class Enrollment << ActiveRecord::Base
belongs_to :academic_class
belongs_to :student
end
class Student << ActiveRecord::Base
has_many :enrollments
has_many :academic_classes, :through => :enrollments
has_many :schools, :through => :academic_classes, :uniq => true
end
班级学校:学术类
有很多:学生,:通过=>:注册,:uniq=>正确
结束
类学术类真实
结束
虽然乍一看,学生应该直接属于班级,但班级并不是真正的“有”和“多”的替代品。为此,我将使用“注册”。(注意,在rails 3.1中,您现在可以执行嵌套:直通调用。)
这里有一个比前一个评论者的实现稍微高级一些的实现:
class School << ActiveRecord::Base
has_many :academic_classes
has_many :enrollments, :through => :academic_classes
has_many :students, :through => :enrollments, :uniq => true
end
class AcademicClass << ActiveRecord::Base
belongs_to :school
has_many :enrollments
end
class Enrollment << ActiveRecord::Base
belongs_to :academic_class
belongs_to :student
end
class Student << ActiveRecord::Base
has_many :enrollments
has_many :academic_classes, :through => :enrollments
has_many :schools, :through => :academic_classes, :uniq => true
end
班级学校:学术类
有很多:学生,:通过=>:注册,:uniq=>正确
结束
类学术类真实
结束
将类
重命名为课程
,因为已经使用了类名类
。加入类,如注册
将处理您的多对多课程-学生关系
class School
has_many :courses
end
class Course
belongs_to :school
has_many :enrollments
has_many :students, :through => :enrollments
end
class Student
has_many :enrollments
has_many :courses, :through => :enrollments
end
class Enrollment
belongs_to :course
belongs_to :student
end
将
类
重命名为课程
,因为已经使用了类名类
。加入类,如注册
将处理您的多对多课程-学生关系
class School
has_many :courses
end
class Course
belongs_to :school
has_many :enrollments
has_many :students, :through => :enrollments
end
class Student
has_many :enrollments
has_many :courses, :through => :enrollments
end
class Enrollment
belongs_to :course
belongs_to :student
end
如果我想在学生档案中列出他们所属的学校,我应该叫student.class.school吗?或者我应该把has\u many:schools,:to=>:classes添加到学生模型中吗?我还希望学生属于多个班级,这样你的解决方案就不起作用了吗?道歉应该是has\u和\u attown\u many。答案已更新。使用
:通过你可以直接打电话给@学校。学生们认为这是行不通的,除非我完全错了。我说我也希望学生成为多所学校的一员。如何添加?但请记住,他们是通过班级属于这些学校的,这会是一个有很多:通过吗?好的,现在我有三个不同的答案,似乎都是有意义的。哪一个是最有效的?如果我想在学生档案中列出他们所属的学校,我应该叫student.class.school吗?或者我应该把has\u many:schools,:to=>:classes添加到学生模型中吗?我还希望学生属于多个班级,这样你的解决方案就不起作用了吗?道歉应该是has\u和\u attown\u many。答案已更新。使用:通过你可以直接打电话给@学校。学生们认为这是行不通的,除非我完全错了。我说我也希望学生成为多所学校的一员。如何添加?但请记住,他们是通过班级属于这些学校的,这会是一个有很多:通过吗?好的,现在我有三个不同的答案,似乎都是有意义的。哪一个效率最高?谢谢!这很有道理。如果我想列出每个学生所在的学校,使用此解决方案我会如何称呼它?我更新了示例,向您展示了如何执行类似以下操作:@student.schoolstanks!这很有道理。如果我想列出每个学生所在的学校,我会如何使用此解决方案来称呼它?我更新了示例,向您展示了如何执行类似以下操作:@student.schools