2011-07-11 6 views
0

モデル:rails 3 guideに基づいて!has_manyフォームからチェックボックスで駆動

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

あなたは少なくとも二つの関係テーブル/モデルが欠けているように私には見えます。

  • SpecialtyDescription
  • あなたはPhysicianSpeciality & PhysicianSpecialityDescription(PhysicianSpeciality_id、SpecialtyDescription_id)& SpecialtyDescription間の右の接続を持つことができる方法PhysicianSpecialityDescription

0

説明はPhysicianSpecialtyの結合レコードの作成を保留にしているので、説明をtext_areaとして渡し、Physicianの保存を待って並べ替えます。

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 
関連する問題