【发布时间】:2013-09-28 14:33:08
【问题描述】:
我正在使用http://guides.rubyonrails.org/ 来学习 ruby 和 rails。我在加入三个表时遇到问题。 ,所以我做了一个新项目作为这个例子:http://guides.rubyonrails.org/association_basics.html#the-has_many_through-association我有三张表医生,约会和病人
型号:
医师.rb
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
attr_accessible :name
end
约会.rb
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
attr_accessible :appointment_date, :patient_id, :physician_id
end
患者.rb
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
attr_accessible :name
end
我想显示患者姓名、医生姓名和约会日期。这个怎么做。 提前致谢。
【问题讨论】:
-
你有为这些模型构建的控制器和视图吗?
-
是的,我有所有三个模型的控制器和视图。
-
那么,您是否只是想知道如何在视图中访问模型及其关联?
标签: ruby-on-rails-3 associations has-many-through