在 ruby on rails 中使用连接表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7606124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using join tables in ruby on rails
提问by user972276
Lets say I have two databases: one for students and one for classes. I would like to be able to 'add' classes to a specific student and also be able to add students to a specific class. I assume I need to use a join table here but I am a little lost on how to use them. I would ultimately like to be able to do something like:
假设我有两个数据库:一个用于学生,一个用于班级。我希望能够为特定学生“添加”课程,也能够将学生添加到特定课程。我假设我需要在这里使用连接表,但我对如何使用它们有点迷茫。我最终希望能够执行以下操作:
@class.students.find(@student_id)
and this would tell me if the student is in the class or not. I know the relationship between classes and students is 'has_many' and vice versa. Does doing 't.references :students' in the migrate files accomplish that? I tried adding that line to my migrate file and then tried finding something using the statement above and it gave me an error. I am new to RoR so I am not even sure what the best way to go about achieving this is. Any help is appreciated!
这会告诉我学生是否在课堂上。我知道班级和学生之间的关系是“has_many”,反之亦然。在迁移文件中执行 't.references :students' 是否可以做到这一点?我尝试将该行添加到我的迁移文件中,然后尝试使用上面的语句查找某些内容,但它给了我一个错误。我是 RoR 的新手,所以我什至不确定实现这一目标的最佳方法是什么。任何帮助表示赞赏!
采纳答案by Jordan Running
Yes, this is a many-to-many relationship (class has many students, student has many classes). For this you'll use a has_many :throughrelation. Take a look at the docs for ActiveRecord::Associations(Ctrl-F for "Association Join Models").
是的,这是一个多对多的关系(班级有很多学生,学生有很多班级)。为此,您将使用has_many :through关系。查看文档ActiveRecord::Associations(Ctrl-F 表示“关联连接模型”)。
In a migration, t.references :studentsis how you would specify a belongs_torelation, as it just adds a student_idcolumn (which can only accommodate one id, i.e. one student). A join model, however, will have two columns: student_idand class_id. (Incidentally, calling a model 'Class' in Ruby is asking for trouble. Might I suggest 'Course'?)
在迁移中,t.references :students您将如何指定belongs_to关系,因为它只添加一student_id列(只能容纳一个 id,即一个学生)。但是,连接模型将有两列:student_id和class_id。(顺便说一句,在 Ruby 中调用模型 'Class' 是在自找麻烦。我可以建议使用 'Course' 吗?)
回答by mliebelt
Everything true what @Jordan said, here the concrete steps to take:
@Jordan 所说的一切都是真的,这里是要采取的具体步骤:
- Create a migration:
rails g model CourseStudentcreates a join model for the n:m relation, and the migration to the corresponding table. Edit the migration file
CreateCourseStudentso it contains the following:class CreateCourseStudent < ActiveRecord::Migration def change create_table :course_students do |t| # Your code comes here t.integer :student_id t.integer :course_id # Here comes the generated code t.timestamps end end endRun the migration:
rake db:migrate. As a result, the join table should now exist in your database.Add to the models the following code
class Course < ActiveRecord::Base has_many :course_students has_many :students, :through => :course_students end class Student < ActiveRecord::Base has_many :course_students has_many :courses, :through => :course_students end class CourseStudent < ActiveRecord::Base belongs_to :student belongs_to :course end
- 创建迁移:
rails g model CourseStudent为 n:m 关系创建连接模型,并迁移到相应的表。 编辑迁移文件
CreateCourseStudent,使其包含以下内容:class CreateCourseStudent < ActiveRecord::Migration def change create_table :course_students do |t| # Your code comes here t.integer :student_id t.integer :course_id # Here comes the generated code t.timestamps end end end运行迁移:
rake db:migrate. 因此,连接表现在应该存在于您的数据库中。将以下代码添加到模型中
class Course < ActiveRecord::Base has_many :course_students has_many :students, :through => :course_students end class Student < ActiveRecord::Base has_many :course_students has_many :courses, :through => :course_students end class CourseStudent < ActiveRecord::Base belongs_to :student belongs_to :course end
You are now able to use the methods generated by the methods belongs_toand has_many:
您现在可以使用方法belongs_to和生成的方法has_many:
@course.students@student.courses
@course.students@student.courses
Try to find all the relevant facts and snippets in the Rails Guides, there you should find all information you need to get on track. Good luck!
尝试在Rails 指南中找到所有相关的事实和片段,在那里您应该可以找到走上正轨所需的所有信息。祝你好运!
回答by StephanieS
This is an old question, but just in case anyone stumbles upon this like I did, you can now have the relationships has_and_belongs_to_many. So yes, you would create a join table:
这是一个老问题,但以防万一有人像我一样偶然发现这个问题,您现在可以拥有这些关系has_and_belongs_to_many。所以是的,您将创建一个连接表:
create_join_table :students, :courses do |t|
t.integer :student_id
t.integer :course_id
end
And then in the models, you would say that a student has_and_belongs_to_many :coursesAnd a course has_and_belongs_to_many :students. There is no need to make a third class called CourseStudent. This linkhas all this information
然后在模型中,你会说一个 studenthas_and_belongs_to_many :courses和一个 course has_and_belongs_to_many :students。没有必要创建一个叫做 CourseStudent 的第三个类。此链接包含所有这些信息

