Note that there are some explanatory texts on larger screens.

plurals
  1. POrails: how to add child association through a text field?
    text
    copied!<p>I have two models: Company and Person</p> <pre><code>class Person &lt; ActiveRecord::Base belongs_to :company end class Company &lt; ActiveRecord::Base has_many :people accepts_nested_attributes_for :people, :allow_destroy =&gt; true, :reject_if =&gt; proc {|attrs| attrs.all? {|k,v| v.blank? } } end </code></pre> <p>And my HTML form partial for new and edit actions looks like this:</p> <pre><code>&lt;% form_for(@company) do |company_f| %&gt; &lt;p&gt; &lt;b&gt;Name&lt;/b&gt;&lt;br /&gt; &lt;%= company_f.text_field :name %&gt; &lt;/p&gt; &lt;ul&gt; &lt;%= render :partial =&gt; 'person_fields', :collection =&gt; @company.people, :locals =&gt; {:company_f =&gt; company_f} %&gt; &lt;%= link_to_add_fields(:people, company_f) %&gt; &lt;/ul&gt; &lt;p&gt; &lt;%= company_f.submit "Submit" %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>where "_person_fields" partial looks like this:</p> <pre><code>&lt;li&gt; &lt;% company_f.fields_for :people, person_fields do |person_f| %&gt; &lt;%= person_f.label :name %&gt; &lt;%= person_f.text_field :name %&gt; &lt;% end %&gt; &lt;/li&gt; </code></pre> <p>At the moment, if I typed in person_f.text_fiel :name the name of the person, and hit save, a new Person model with that name gets created. Not what I want at all, I already HAVE that person's Person model in the database, I rather want to ASSOCIATE this person to this company. </p> <p>Another thing is that I wouldn't mind using the name for human-friendly identification of the person rather than id like this for the "_person_fields" partial</p> <pre><code>&lt;li&gt; &lt;% company_f.fields_for :people, person_fields do |person_f| %&gt; &lt;%= person_f.label :id %&gt; &lt;%= person_f.text_field :id %&gt; &lt;% end %&gt; &lt;/li&gt; </code></pre> <p>this by the way, doesn't work either. when I hit submit, nothing happens. nothing gets saved or changed or anything. </p> <p>So I thought, just for the sake of experiment, say I did use id's for identification for a Person model, (so that I don't have to go in to autocomplete with a hidden id field which I am using for another project. I hate it). All I want is: go to a new/edit Company page, there's a bunch of textfields for me to type in ids of people, and save and then these people are then associated with the company. I mean, it's exactly like</p> <pre><code>people = Person.find(1,2,3) #=&gt;["romeo","juliet","henry"] company = Company.first #=&gt;["Shakespeare Co."] company.people&lt;&lt;people company.people #=&gt;["romeo","juliet","henry"] </code></pre> <p>And it'd be best if I didn't have to use select menus because eventually if the project takes off and I have a thousand people, that's too big for any select menu. I know then I will have to use autocomplete + hidden id field that gets set when a person's name is chosen.</p> <p>Thanks!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload