Rails has_many:通过保存其他字段
我试图在约会模型(下面)上找到一种优雅的方法来保存一个名为描述的附加字段。 我的模型设置如下:
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
在我看来,我有复选框设置来保存连接表的数据,但我想在另一个“描述”字段中滑动以与连接一起保存。 以下是我认为的内容:
您可以使用accepts_nested_attributes_for
更新关联属性。
在模型中:
accepts_nested_attributes_for :appointments, :allow_destroy => true
在视图中:
<%= f.fields_for :appointments do |apt| %> <%= apt.object.patient.name %> <%= apt.text_field :description %> <% end %>
请参阅http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html