【发布时间】:2014-08-07 18:37:20
【问题描述】:
我正在尝试实现http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association中的示例
但我不确定正确的方法是什么?您是否先创建迁移,然后通过更改模型在模型中应用 has many through?
hospital$ rails g model Physician name:string
invoke active_record
create db/migrate/20140807183053_create_physicians.rb
create app/models/physician.rb
invoke test_unit
create test/models/physician_test.rb
create test/fixtures/physicians.yml
hospital$ rails g model Patient name:string
invoke active_record
create db/migrate/20140807183112_create_patients.rb
create app/models/patient.rb
invoke test_unit
hospital$ rails g model Appointment physician:references patient:references appointment_date:datetime
invoke active_record
create db/migrate/20140807183152_create_appointments.rb
create app/models/appointment.rb
invoke test_unit
create test/models/appointment_test.rb
create test/fixtures/appointments.yml
这些是生成的模型:
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
end
class Physician < ActiveRecord::Base
end
是否有关于实施 has many through 模型的分步指南?该指南解释了这个想法,我理解它,但我不确定实际实施它的正确方法,或者它是否重要?我很困惑
【问题讨论】:
标签: activerecord ruby-on-rails-4