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/3/reactjs/27.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_Activerecord_Model Associations - Fatal编程技术网

Ruby on rails 对rails活动记录关联感到困惑

Ruby on rails 对rails活动记录关联感到困惑,ruby-on-rails,activerecord,model-associations,Ruby On Rails,Activerecord,Model Associations,4模型在其表中有以下列 student : student_id name dept semester: semester_id semester_name subject : subject_id subject_name semester_id result : student_id semester_id result_id result 我将这些模型关联为 student: has_many semesters has_many subjects

4模型在其表中有以下列

student : student_id  name    dept
semester: semester_id semester_name
subject : subject_id  subject_name  semester_id
result  : student_id  semester_id   result_id result
我将这些模型关联为

student: has_many semesters
         has_many subjects through semesters
         has_many results through semesters

semester: belongs_to students
          has_many subjects
          has_many results

subject: belongs_to semesters
         has_many student through semesters

result: has_many students through semesters
        belongs_to semesters
如果可以,请告诉我。我不知道什么时候使用它。我的意思是,我可以用下面这样的东西吗

student: has_many semesters
         has_many subjects through semesters            

semester: has_many students
          has_many subjects              

subject: has_many semesters
         has_many student through semesters 

正确的方法是:

Student: has_many :semesters
         has_many :subjects, through: semesters            

Semester: belongs_to :student
          belongs_to :subject              

Subject: has_many :semesters
         has_many :students, through: :semesters 
您需要进行以下迁移:

drop_table :results # you won't need it anymore if you use has_many through
remove_column :students, :student_id, :integer
add_column :students, :id, :integer # if you don't have the ID column in your students table
remove_column :subjects, :subject_id, :integer
add_column :subjects, :id, :integer
add_reference :semesters, :student, index: true
add_reference :semesters, :subject, index: true

该类的定义始终是具有外键声明的对应db表。

我已更新了表列。请现在检查,并让我知道如何在更新表列后进行关联。请现在检查,并让我知道我可以如何做这项工作association@user1478137您的表列似乎有很多问题。让我们澄清一下你想做什么。我想我们应该从下学期开始。1该系统有许多学期。2每学期都有很多科目。三。每门课都会有很多学生。4对每个学生来说,他所学的每门学科都会有一个结果。这些是对的吗?