嗨,我希望能够在Rails控制台中定义has_one / belongs_to关系。我正在使用Rails 4(如果这很重要)。在通读了其他一些线程之后,我尝试了一些方法,但是这里首先是我的模型。
class Appointment < ActiveRecord::Base
belongs_to :patient
end
和
class Patient < ActiveRecord::Base
has_one :appointments
end
我已经看到可能需要创建一个attr_accessor,但是我的讲师在任何一个类中都可以在没有它的情况下进行创建,并且我想在可能的情况下进行模拟。我试过了:
a = Appointment.create(date: "9/9/15", patient_id: 2)
=> ActiveRecord::UnknownAttributeError: unknown attribute 'patient_id' for Appointment.
和
a.patient = s //after defining both (s being patient)
=> ActiveModel::MissingAttributeError: can't write unknown attribute `patient_id`
以及其他一些方法,但是我将为您省去所有的复制粘贴。我该怎么做呢?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句