Assigning a Formtastic Nested Attribute

I have been trying to figure this out for a while, but still no luck. I have a company_relationships table that joins Companies and People , keeping an extra field to describe the nature of the relationship called corp_credit_id. I can make the forms work fine to add company_relationships for Person, but I cannot figure out how to set this modifier field in doing so. Any ideas?

More about my project: people have many companies through company_relationships . With this extra field, I use it to group all the specific relationships. Therefore, I can group the person "Doctors", "Contractors", etc.

My models:

Company.rb (abbreviated)

class Company < ActiveRecord::Base
   include ApplicationHelper

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

Person.rb (abbreviated)

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 is 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 I would like to do something like: corp_credit_id => '1' in this input to assign this attribute to Coffee Shop. But formtastic does not seem to allow this task.

Any ideas on how to do this?

+3
source share
1 answer

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 %>

In the documentation

+1
source

Source: https://habr.com/ru/post/1771608/


All Articles