我一直试图弄清楚这一点,但仍然没有运气。我有一个连接Companies和People的 company_relationships 表,存储一个额外的字段来描述称为“corp_credit_id”的关系的性质。我可以让表单正常工作,为一个人添加 company_relationships,但我似乎不知道如何在这样做时设置该修饰符字段。有任何想法吗?
关于我的项目的更多信息:人们通过company_relationships拥有许多公司拥有许多公司。有了那个额外的字段,我用它来将所有特定的关系组合在一起。所以我可以对一个人的医生、承包商等进行分组。
我的模型:
公司.rb (删节)
class Company < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :people, :through => :company_relationships
Person.rb(删节)
class Person < ActiveRecord::Base
include ApplicationHelper
has_many :company_relationships
has_many :companies, :through => :company_relationships
accepts_nested_attributes_for :company_relationships
company_relationship.rb
class CompanyRelationship < ActiveRecord::Base
attr_accessible :company_id, :person_id, :corp_credits_id
belongs_to :company
belongs_to :person
belongs_to :corp_credits
end
我的表格部分,使用formtastic。
<% semantic_form_for @person do |f| %>
<%= f.error_messages %>
<% f.inputs do %>
...
<%= f.input :companies, :as => :check_boxes, :label => "Favorite Coffee Shops", :label_method => :name, :collection => Company.find(:all, :conditions => {:coffee_shop => 't'}, :order => "name ASC"), :required => false %>
所以我想做的是像 :corp_credit_id => '1' 在那个输入中为咖啡店分配那个属性。但是 formtastic 似乎不允许这种分配发生。
关于如何做到这一点的任何想法?