views:

40

answers:

1

I've been trying to figure this one out for a while but still no luck. I have a company_relationships table that joins Companies and People, storing an extra field to describe the nature of the relationship called 'corp_credit_id'. I can get the forms working fine to add company_relationships for a Person, but I can't seem to figure out how to set that modifier field when doing so. Any ideas?

More about my project: People have many companies through company_relationships. With that extra field in there I am using it to group all of the specific relationships together. So I can group a person's Doctors, Contractors, etc.

My models:

Company.rb (abridged)

class Company < ActiveRecord::Base
   include ApplicationHelper

has_many :company_relationships
has_many :people, :through => :company_relationships

Person.rb (abridged)

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

My form partial, using 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 %>

So what I would like to do is something like :corp_credit_id => '1' in that input to assign that attribute for Coffee Shop. But formtastic doesn't appear to allow this assignment to happen.

Any ideas on how to do this?

A: 

Are you looking for something like

  <% semantic_form_for @person do |form| %>
    <% form.semantic_fields_for :company_relationships do |cr_f| %>
    <%= cr_f.input :corp_credit_id  %>
<% end %>

It is in the documentation

monocle
I've tried that approach and I end up with it displaying several check_boxes, depending on how many existing company relationships there are. What I would like to do is have one check_box or select box that allows for multiple selections. On submitting, it should update company_relationships and store a "1" in the corp_credit_id field in the case of coffee shops. (other numbers for other types of relationships).
Scott Allen