Rails has_many :through 保存额外字段

5

我试图找到一种优雅的方法,在Appointment模型(如下所示)上保存一个名为“description”的附加字段。我的模型设置如下:

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, through: :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Patients < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, through: :appointments
  attr_accessible :name
end

我认为我已经设置了复选框以保存连接表的数据,但我想添加一个额外的“描述”字段来与连接一起保存。以下是我的视图中的内容:

<div class="field">
  <fieldset>
  <legend>Patients</legend>
  <% @patients.each_slice(2) do |slice| %>
    <div class='row'>
      <% slice.each do |patient| %>
        <div class='span3'>
          <%= label_tag "physician_patient_ids_#{patient.id}" do %>
            <%= check_box_tag 'physician[patient_ids][]', patient.id,
                              @physician.patients.include?(patient),
                              { id: "physician_patient_ids_#{patient.id}" } %>
            <%= patient.name %>
          <% end %>
          <!-- need to add in description here somehow -->
        </div>
      <% end %>
    </div>
  <% end %>
  </fieldset>
</div>

患者模型是否缺少了has_many关联? - SG 86
是的,那是我的失误。关系是存在的,我会编辑以反映出来。 - lscott3
1个回答

2

不要忘记在控制器中的 strong_params 中添加必要的参数。 - Mic Fok

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接