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.
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:
table: skills
+-----------+----------------+
| user_id | skill |
+-----------+----------------+
| 4 | Photoshop |
+-----------+----------------+
| 4 | Illustrator |
+-----------+----------------+
| 4 | InDesign |
+-----------+----------------+
Now, what I want to do is NOT store the actual skill names. Instead, I want to store the ID of the skill from skills_vocabulary
list (which is what the jquery-autocomplete uses).
table: skills_vocabulary
+------+----------------+
| id | skill |
+------+----------------+
| 1 | Illustrator |
+------+----------------+
| 2 | Dreamweaver |
+------+----------------+
| 3 | After Effects |
+------+----------------+
| 4 | InDesign |
+------+----------------+
| 5 | Photoshop |
+------+----------------+
| 6 | Flash |
+------+----------------+
THUS, the skills
table should look like this:
+-----------+----------------+
| user_id | skill_id |
+-----------+----------------+
| 4 | 5 |
+-----------+----------------+
| 4 | 1 |
+-----------+----------------+
| 4 | 4 |
+-----------+----------------+
Any help would be greatly, greatly, greatly appreciated. I've been at war with this for an embarrassing 3 months.
Thank you all.
EDIT/
<%= f.fields_for :skills do |builder| %>
<%= render "skills_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add a Skill", f, :skills %></p>
_skills_fields.html.erb
<div class="fields ac skills">
<div class="clearfix">
<span class="left"><%=f.autocomplete_field :name, users_autocomplete_skills_vocab_name_path, :class => 'restricted_to_autocomplete' %></span><span class="remove_link left"><%= link_to_remove_fields "[x]", f %></span>
</div>
</div>
EDIT 2/
Right now it's just
skill.rb
class Skill < ActiveRecord::Base
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
has_many :tskills, :dependent => :destroy
accepts_nested_attributes_for :tskills, :allow_destroy => true, :reject_if => lambda { |a| a[:name].blank? }
end
skills_vocabulary.rb
class SkillsVocabulary < ActiveRecord::Base
end
My controller code is the basic index, show, new, create, update, destroy
The list of skills (about 10,000 items long) is in the table skills_vocabularies