use*_*457 -5 ruby activerecord ruby-on-rails ruby-on-rails-4
更改我的代码!我有以下错误: PG::UndefinedColumn: 错误: 列 Students.year_course_id 不存在 第 1 行: SELECT COUNT( ) FROM "students" WHERE "students"."year_cour... ^ : SELECT COUNT( ) FROM "students " 其中“学生”."year_course_id" = $1
PG::UndefinedColumn: 错误: 列 Students.year_course_id 不存在 第 1 行: SELECT COUNT() FROM "students" WHERE "students"."year_cour... ^ : SELECT COUNT() FROM "students" WHERE "students"。 “year_course_id”= $1
错误明确指出表year_course_id
中不存在该列students
。创建它来解决问题。
当您指定
class Student < ActiveRecord::Base
belongs_to :year_course
end
Run Code Online (Sandbox Code Playgroud)
您需要在引用表的表year_course_id
中添加字段。students
year_courses
更新
以下是如何通过生成迁移year_course_id
在表中添加列:students
rails generate migration AddYearCourseRefToStudents year_course:references
Run Code Online (Sandbox Code Playgroud)
这次运行后rake db:migrate