【发布时间】:2016-02-19 09:37:45
【问题描述】:
以下代码来自http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
class CreateAppointments < ActiveRecord::Migration
def change
create_table :physicians do |t|
t.string :name
t.timestamps null: false
end
create_table :patients do |t|
t.string :name
t.timestamps null: false
end
create_table :appointments do |t|
t.belongs_to :physician, index: true
t.belongs_to :patient, index: true
t.datetime :appointment_date
t.timestamps null: false
end
结束 结束
在上面的例子中我是怎么做的:
1) 创建/破坏医生和患者之间的关系。我只是使用:
Create: Appointment.create(physician_id, patient_id)
Destroy: (i have no clue hot to do this)
正确的做法是什么?
2) 我如何访问特定患者或医生的预约模型中的所有预约日期?
【问题讨论】:
-
我目前正在使用 create 创建关系。至于销毁,我一直在销毁控制台上的记录。 (如果这真的很基本,我对 Rails 很陌生)
-
我刚刚在另一个问题上找到了以下创建关系:@course.topics
-
您的代码 sn-p 仅将新主题分配给名为
@course的临时变量。因此,尽管该主题现在是@course的一部分,并且可以通过@course.topics访问,但没有持久的关系。
标签: ruby-on-rails