【问题标题】:SQLite3 error in rails SQLite3::SQLException: no such columnRails SQLite3::SQLException 中的 SQLite3 错误:没有这样的列
【发布时间】:2016-03-12 11:51:24
【问题描述】:

我正在制作一个 Rails 应用程序,用户可以在其中发布课程并订阅课程。我的模型文件是:
user.rb:

class User < ActiveRecord::Base
  has_many :courses, dependent: :destroy
  has_many :coursejoins, dependent: :destroy
  has_many :learnables, through: :coursejoins, class_name: "Course"
  has_many :comments, dependent: :destroy
  ...
end

course.rb:

class Course < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :coursejoins, dependent: :destroy
  has_many :students, through: :coursejoins, class_name: "User"
  has_many :documents, dependent: :destroy
  has_many :comments, dependent: :destroy
  ...
end

coursejoin.rb:

class Coursejoin < ActiveRecord::Base
  belongs_to :learnable, :class_name => "Course"
  belongs_to :student, :class_name => "User"
end

20160219171527_create_coursejoins.rb

class CreateCoursejoins < ActiveRecord::Migration
  def change
    create_table :coursejoins do |t|
      t.boolean :accepted
      t.timestamps
    end
  end
end

20160219174937_add_student_id_to_coursejoins.rb

class AddStudentIdToCoursejoins < ActiveRecord::Migration
  def change
    add_column :coursejoins, :student_id, :integer
  end
end

20160219224309_add_learnable_id_to_coursejoins.rb

class AddLearnableIdToCoursejoins < ActiveRecord::Migration
  def change
    add_column :coursejoins, :learnable_id, :integer
 end

结束

coursejoin 模型就像是课程和用户之间的关系。
当我尝试做@course.students 时,出现错误:

SELECT "users".* FROM "users" INNER JOIN "coursejoins" ON "users"."id" = "coursejoins"."student_id" WHERE "coursejoins"."course_id" = ?  [[nil, 1]]
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: coursejoins.course_id: SELECT "users".* FROM "users" INNER JOIN "coursejoins" ON "users"."id" = "coursejoins"."student_id" WHERE "coursejoins"."course_id" = ?

我不太了解 SQL,因此无法解读错误。
我查看了具有类似错误的问题,但似乎没有一个相关。
我已经坚持了两天了。
我该如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord sqlite


    【解决方案1】:

    好的,我解决了这个问题,并认为我应该发布解决方案以供将来参考。基本上所有需要做的就是添加

    foreign_key: "learnable_id"
    "has_many :coursejoins,dependent: :destroy"
    在课程模型和
    foreign_key: "student_id"
     has_many :coursejoins,dependent: :destroy
    到用户模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-19
      • 2022-08-15
      • 2013-10-09
      • 1970-01-01
      • 2020-10-18
      • 2015-10-08
      • 1970-01-01
      • 2017-02-22
      相关资源
      最近更新 更多