accept nested attributes for has_many relationship

后端 未结 3 819
谎友^
谎友^ 2020-12-19 16:40

Below are my two model classes

class Patient < ActiveRecord::Base
  belongs_to :user, :dependent => :destroy
  has_many :enrollments, :dependent =>          


        
3条回答
  •  时光取名叫无心
    2020-12-19 16:46

    In Rails 3 (not sure about previous versions) you don't even need to use accepts_nested_attributes_for to accomplish this. You can simply remove all the view code you listed and replace it with the following:

    <%= patient_form.select(:client_ids, @clients.collect {|c| [ c.name, c.id ] }, {}, {:multiple => true})%>
    

    Rails will do its magic (because of you named the select "client_ids") and it will just work.

提交回复
热议问题