Ruby on rails 我有以下错误:PG::UnfinedColumn:错误:column students.year\u course\u id不存在第1行

Ruby on rails 我有以下错误:PG::UnfinedColumn:错误:column students.year\u course\u id不存在第1行,ruby-on-rails,ruby,activerecord,ruby-on-rails-4,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 4,更改我的代码!我有以下错误:PG::UnfinedColumn:错误:column students.year\u course\u id不存在第1行:从“students”中选择COUNT(),其中“students”。“year\u cour…^:从“students”中选择COUNT(),其中“students”。“year\u course\u id”=$1 PG::UndefinedColumn:错误:column students.year\u course\u id不存在 存在第1

更改我的代码!我有以下错误:PG::UnfinedColumn:错误:column students.year\u course\u id不存在第1行:从“students”中选择COUNT(),其中“students”。“year\u cour…^:从“students”中选择COUNT(),其中“students”。“year\u course\u id”=$1

PG::UndefinedColumn:错误:column students.year\u course\u id不存在 存在第1行:从“学生”中选择COUNT(),其中 “学生们”本学年…^:从“学生”中选择COUNT(),其中 “学生”。“年级课程id”=1美元

错误清楚地表明
year\u course\u id
列在
students
表中不存在。 创建它以解决问题

当您指定

class Student < ActiveRecord::Base
  belongs_to :year_course
end

在这次运行之后,
rake db:migrate

Kirti的答案是正确的,因为您肯定需要在相应的引用模型中有一个id列,但是,如果您已经准备好了id列,但仍然得到了错误(像我一样),那么下面是我如何解决它的:

我发现我试图在我的模型中将一个不同的名称委托给列关联,这是由于某种原因导致的。也就是说,我有:

has_many :document_doc_types, as: :doc_types
Rails发出了与您发布的内容类似的错误。在我删除了“as:doc_types”部分后,一切又恢复了正常。因此,这是可以预期的:

has_many :document_doc_types

希望这能有所帮助。

是否有可能解决另一个问题?要将这些行替换为其他行:@student\u count=@year\u courses?..
@year\u courses。除非您在
students
表中有
year\u courses\u id
,否则学生将无法工作。查看我关于如何添加它的更新答案。我在表中有一个学生的现场课程id,我更正了它,然后它起作用了吗?如果没有,您会得到什么错误?另外,从
schema.rb
中共享
学生
表的模式。在问题中加上它写这一行的正确性。
has_many :document_doc_types