我正在为一个
: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
此外,在任何排列中,我给他们一个失败的命名错误例外:
undefined method `person_id' for #<Array:0x82e7db0>
就好像
person_id
字段不存在,但我可以使用
会计人员
和
:个人ID
非常好。