【发布时间】:2018-10-21 00:44:47
【问题描述】:
我正在为我的一门课程开发一个 Ruby on Rails 项目,我当前的任务要求我允许用户注册课程。我有三个模型,用户、课程和属于两者的关联连接表,即注册。当前各模型的rb文件中的设置如下:
class Enrollment < ApplicationRecord
belongs_to :user
belongs_to :course
end
class Course < ApplicationRecord
has_many :enrollments
has_many :users, through: :enrollments
end
class User < ApplicationRecord
has_many :enrollments
has_many :courses, through: :enrollments
end
主要问题是,每当我尝试从 rails 控制台向 Enrollment 模型添加新条目时,都会出现错误提示:
[4] pry(main)> enrollment = Enrollment.new(u_id: 1, c_id: 1)
=> #<Enrollment:0x00007f94bc771020 id: nil, u_id: 1, c_id: 1, ...>
[5] pry(main)> enrollment.save
(0.1ms) begin transaction
(0.1ms) rollback transaction
=> false
[6] pry(main)> enrollment.errors
=> #<ActiveModel::Errors:0x00007f94bc7c7290
@base=#<Enrollment:0x00007f94bc771020 id: nil, u_id: 1, c_id: 1, created_at:
nil, updated_at: nil>,
@details={:user=>[{:error=>:blank}], :course=>[{:error=>:blank}]},
@messages={:user=>["must exist"], :course=>["must exist"]}>
它声称用户和课程必须存在,但两个模型都已填充并单独工作。但是,通过将我的 registration.rb 更改为:
class Enrollment < ApplicationRecord
belongs_to :user, optional: true
belongs_to :course, optional: true
end
它允许我添加一个新的注册条目就好了。有谁知道为什么我需要“可选:真”才能使其工作?有谁知道在不使用“可选:真”的情况下解决此问题的方法?任何帮助,将不胜感激。我也愿意详细说明,所以请告诉我。
谢谢!
【问题讨论】:
标签: ruby-on-rails database activerecord