模特:基于rails 3 guide!
class Physician < ActiveRecord::Base
has_many :physician_specialities
has_many :specialities, :through => :physician_specialities
end
class speciality < ActiveRecord::Base
has_many :physician_specialities
has_many :physicians, :through => :physician_specialities
end
class PhycianSpeciality < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
数据库架构如下所示:
Physician
id
name
Speciality
id
name
PhycianSpeciality
id
physician_id
speciality_id
description
我想要一个能够为医生添加专业的表格,并写一个关于这个专业的小描述(根据医生的说法)。
我想我可以使用像Quick Tip: has_many :through => checkboxes这样的东西!
<% form_for @physician do -%>
<% Speciality.all.each do |group| -%>
<div>
<%= check_box_tag :speciality_ids, speciality.id, @user.specialities.include?(speciality), :name => 'user[speciality_ids][]' -%>
<%= label_tag :speciality_ids, speciality.name -%>
</div>
<% end -%>
<%= submit_tag -%>
<% end -%>
但我不知道在哪里可以提供专业描述......
答案 0 :(得分:0)
在我看来,你至少缺少两个关系表/模型。
通过这种方式,您可以在PhysicianSpeciality&amp; PhysicianSpecialityDescription(PhysicianSpeciality_id,SpecialtyDescription_id)&amp; SpecialtyDescription。
答案 1 :(得分:0)
由于描述正在等待在PhysicianSpecialty中创建连接记录,所以我只是将描述作为text_area传递并在医生保存之前对其进行排序。
physicians_controller.rb
# i imagine ur update method would look kinda like this
def update
@physician = Physician.find(params[:id])
if @physician.update_attributes(params[:physician])
physician_specialty = PhysicianSpecialty.find_by_specialty_id(:specialty_id => specialty.id)
physician_specialty.update_attribute(:description, params[:description])
else
render :action => :edit
end
end