Note that there are some explanatory texts on larger screens.

plurals
  1. POmany_to_many associations in rails
    primarykey
    data
    text
    <p>This has been a nightmare for me and I'm willing to give an arm and an eyeball to the person who can help me :P I'm going to explain this as best as possible.</p> <p>When a user edits their 'profile' they can type in skills that they have in nested attributes fields (they can click 'Add Another Skill' and it will populate another field for them to type). When they begin to type, jquery-autocomplete kicks in. They MUST select an item from autocomplete. When they save, for example, 3 skills, we get this:</p> <p>table: <code>skills</code></p> <pre><code>+-----------+----------------+ | user_id | skill | +-----------+----------------+ | 4 | Photoshop | +-----------+----------------+ | 4 | Illustrator | +-----------+----------------+ | 4 | InDesign | +-----------+----------------+ </code></pre> <p>Now, what I want to do is NOT store the actual skill names. Instead, I want to store the ID of the skill from <code>skills_vocabulary</code> list (which is what the jquery-autocomplete uses).</p> <p>table: <code>skills_vocabulary</code></p> <pre><code>+------+----------------+ | id | skill | +------+----------------+ | 1 | Illustrator | +------+----------------+ | 2 | Dreamweaver | +------+----------------+ | 3 | After Effects | +------+----------------+ | 4 | InDesign | +------+----------------+ | 5 | Photoshop | +------+----------------+ | 6 | Flash | +------+----------------+ </code></pre> <p>THUS, the <code>skills</code> table should look like this:</p> <pre><code>+-----------+----------------+ | user_id | skill_id | +-----------+----------------+ | 4 | 5 | +-----------+----------------+ | 4 | 1 | +-----------+----------------+ | 4 | 4 | +-----------+----------------+ </code></pre> <p>Any help would be greatly, greatly, greatly appreciated. I've been at war with this for an embarrassing 3 months.</p> <p>Thank you all.</p> <p>EDIT/</p> <pre><code>&lt;%= f.fields_for :skills do |builder| %&gt; &lt;%= render "skills_fields", :f =&gt; builder %&gt; &lt;% end %&gt; &lt;p&gt;&lt;%= link_to_add_fields "Add a Skill", f, :skills %&gt;&lt;/p&gt; </code></pre> <p>_skills_fields.html.erb</p> <pre><code>&lt;div class="fields ac skills"&gt; &lt;div class="clearfix"&gt; &lt;span class="left"&gt;&lt;%=f.autocomplete_field :name, users_autocomplete_skills_vocab_name_path, :class =&gt; 'restricted_to_autocomplete' %&gt;&lt;/span&gt;&lt;span class="remove_link left"&gt;&lt;%= link_to_remove_fields "[x]", f %&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>EDIT 2/</p> <p>Right now it's just</p> <p>skill.rb</p> <pre><code>class Skill &lt; ActiveRecord::Base belongs_to :user end </code></pre> <p>user.rb</p> <pre><code>class User &lt; ActiveRecord::Base has_many :tskills, :dependent =&gt; :destroy accepts_nested_attributes_for :tskills, :allow_destroy =&gt; true, :reject_if =&gt; lambda { |a| a[:name].blank? } end </code></pre> <p>skills_vocabulary.rb</p> <pre><code>class SkillsVocabulary &lt; ActiveRecord::Base end </code></pre> <p>My controller code is the basic index, show, new, create, update, destroy</p> <p>The list of skills (about 10,000 items long) is in the table <code>skills_vocabularies</code></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.
 

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