0
我建立一個形式爲:has_many :through
關係:NoMethodError與collection_select
class Account < ActiveRecord::Base
has_many :employments
has_many :people, :through => :employments
accepts_nested_attributes_for :employments
end
class Person < ActiveRecord::Base
has_many :employments
has_many :accounts, :through => :employments
end
class Employment < ActiveRecord::Base
belongs_to :account
belongs_to :person
end
的Employment
模型包含字段:account_id
和:person_id
。
在帳戶形式,我加入:
<% fields_for 'account[employments_attributes][]', @account.employments do |e| %>
<%= e.hidden_field :account_id, :value => @account.id %>
<%= e.collection_select :person_id, Person.all, :id, :name %>
<% end %>
collection_select
,或select
爲好,以任何排列我給他們失敗了NoMethodError例外:
undefined method `person_id' for #<Array:0x82e7db0>
這是因爲如果person_id
字段不存在,但我可以用:account_id
和:person_id
完美地調用創建方法。