我正在建立一个:has_many :through关系表格:
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字段不存在,但我可以调用 create 方法:account_id并且:person_id非常好。