Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>accepts_nested_attributes_for :people</code> defines <code>people_attributes</code> and <code>people_attributes=</code> methods in your Company model. Those two methods are used when you have <code>fields_for :people</code> in form. As stated in <a href="http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#M002132" rel="nofollow noreferrer">documentation</a> (read whole page, not only method definition):</p> <blockquote> <p>Nested attributes allow you to save attributes on associated records through the parent.</p> </blockquote> <p>It doesn't work to associate two objects, that can be done without it, you still have <code>people</code> method in Company.</p> <p><a href="http://github.com/justinfrench/formtastic" rel="nofollow noreferrer">Formtastic</a> for example uses select with multiple attribute (you can check more than one option with ctrl) or list of check_boxes (easier for normal user, don't have to touch keyboard).</p> <p>If you want to use autocomplete, it could be done, but you need to append your ids to <code>people[]</code> array (don't remember format right now, I'll check later. You can check its format when you create attribute in your form for <code>:people</code> I think)</p> <p><em>Edit:</em><br> I think I know hot to do it simple way (without autocomplete, simple html only).</p> <p>In your <code>companies/edit</code> (or <code>new</code>) view place:</p> <pre><code>&lt;p&gt; &lt;%= company_f.label :people %&gt; &lt;%= company_f.collection_select :person_ids, Person.all, :id, :name, {}, {:multiple =&gt; true} %&gt; &lt;/p&gt; </code></pre> <p>That will allow you to select multiple people to company (with Ctrl). Params generated by this part transfered to your controller should now look like:</p> <pre><code>"company"=&gt;{"name"=&gt;"CompanyA", "person_ids"=&gt;["1", "3"]}, "commit"=&gt;"Update", "id"=&gt;"4" </code></pre> <p>If you want to show people with checkboxes I managed to get it with this (I copied part of markup from <a href="http://github.com/justinfrench/formtastic" rel="nofollow noreferrer">formtastic</a>):</p> <pre><code>&lt;ul&gt; &lt;% Person.all.each do |person| %&gt; &lt;li&gt; &lt;%= check_box :person, :id, {:name =&gt; "company[person_ids][]", :checked =&gt; @company.people.include?(person) }, person.id, nil %&gt; &lt;%= person.name %&gt; &lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; </code></pre> <p>I had no time today to check how to do it with hidden fields, so you can use autocomplete, but I believe that it should be similar to checkboxes - you need to use something which adds <code>&lt;input type="hidden" name="company[person_ids][]" value="#{person.id}"&gt;</code> for every autocompleted person, and also need to create initial list of people already in company (<strong>YOU OVERRIDE THE LIST</strong>, not add to list).</p> <hr> <p>If you want field to add or remove (separate fields, sorry) people from company, then this should work:</p> <p>in Company model:</p> <pre><code>def add_people=(people_ids) ids = people_ids.split(/,/).map(&amp;:to_i) person_ids += ids if ids end def add_people "" end def remove_people=(people_ids) ids = people_ids.split(/,/).map(&amp;:to_i) person_ids -= ids if ids end def remove_people "" end </code></pre> <p>and in your new/edit view:</p> <pre><code>&lt;p&gt; &lt;%= f.label :add_people %&gt;&lt;br /&gt; &lt;%= f.text_field :add_people %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :remove_people %&gt;&lt;br /&gt; &lt;%= f.text_field :remove_people %&gt; &lt;/p&gt; </code></pre> <p>Now all you need to do is to find JavaScript for auto-complete, connect it to ids and names of all people (probably <code>PeopleController#index, :format =&gt; :json</code> should be good) and tell it to fill those text_fields (can be hidden fields if you would use autocomplete).</p> <p>This approach should work, because you define virtual attributes in company model, and by assigning to them string in format <code>"1, 2, 6"</code>, you add/remove those ids from your collection of associated people</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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