I'm building a form for a :has_many :through
relationship:
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
The Employment
model contains the fields :account_id
and :person_id
.
Within the account form, I'm adding:
<% 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
, or select
as well, in any permutation I give them fail with a NoMethodError exception:
undefined method `person_id' for #<Array:0x82e7db0>
It's as if the person_id
field doesn't exist and yet I can call the create method with :account_id
and :person_id
perfectly fine.