title: Save the id of autocomplete field instead of the text (Jquery autocomplete) in Rails3
body: <p>I have managed to setup autocomplete field for my rails app.  What I'm trying to do is to have the contents of a lookup table listed and when I have selected the lookup text for few lines I would like the id of the lookup to be saved instead of the text itself.  Here is what I have done:</p>
  
  <p>Recipe model:</p>
  
  <pre><code>class Recipe &lt; ActiveRecord::Base
    # validates :name,  :presence =&gt; true
    # validates :directions, :presence =&gt; true
  
    # has_and_belongs_to_many :ingradients
  
    has_many :ingredients_recipes
    has_many :ingredients, :through =&gt; :ingredients_recipes
  
    accepts_nested_attributes_for :ingredients_recipes, :allow_destroy =&gt; true
  end
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>class RecipesController &lt; ApplicationController
    # autocomplete :ndb_food_des, :long_desc
    autocomplete :ndb_food_de, :long_desc, :limit =&gt; 15
  
    ... more lines
  end
  </code></pre>
  
  <p>I then use this in the view:</p>
  
  <pre><code> &lt;% f.fields_for :ingredients_recipes do |rif| %&gt;        
            &lt;td&gt;            
                &lt;%= rif.autocomplete_field :ingredient_id, recipes_autocomplete_ndb_food_de_long_desc_path, :width=&gt;1000, :size=&gt;60 %&gt;
            &lt;/td&gt;  
  &lt;% end %&gt;
  </code></pre>
  
  <p>I have three detail lines generated when the view opens and what I want to do is write the id of the selected item from the autocomplete.  If I specify the correct field in the view as ingredient_id then I always get 0 in the id in the insert.  If I specify some other field I get the full text of the autocomplete, so it seems that Rails is picking up the correct field and seems to know what to do ... but it's not getting the correct id.</p>
  
  <p>Here is a trace of what the system does:</p>
  
  <pre><code>Started POST "/recipes" for 127.0.0.1 at 2010-10-17 16:28:10 +0000
    Processing by RecipesController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"wmNQbsGoiGccrNpmFs7r7D7ZBq//SBe9yrk6SFXP0lc=", "recipe"=&gt;{"name"=&gt;"dfsa", "description"=&gt;"sdfa", "directions"=&gt;"sadf", "ingredients_recipes_attributes"=&gt;{"0"=&gt;{"ingredient_id"=&gt;"CAMPBELL Soup Company, CAMPBELL'S Beef Gravy", "readable_qty"=&gt;"30", "description"=&gt;""}, "1"=&gt;{"ingredient_id"=&gt;"Eclairs, custard-filled with chocolate glaze, prepared from recipe", "readable_qty"=&gt;"60", "description"=&gt;""}, "2"=&gt;{"ingredient_id"=&gt;"DENNY'S, MOONS &amp; STARS chicken nuggets, from kid's menu", "readable_qty"=&gt;"10", "description"=&gt;""}}, "servings"=&gt;"3", "time"=&gt;"3"}, "commit"=&gt;"Create Recipe"}
    SQL (27.9ms)  INSERT INTO "recipes" ("created_at", "description", "directions", "name", "owner", "servings", "time", "updated_at") VALUES ('2010-10-17 16:28:10.686644', 'sdfa', 'sadf', 'dfsa', NULL, 3, 3, '2010-10-17 16:28:10.686644')
    SQL (0.2ms)  INSERT INTO "ingredients_recipes" ("created_at", "description", "ingredient_id", "qty", "recipe_id", "updated_at") VALUES ('2010-10-17 16:28:10.729542', '', **0,** 32.0, 18, '2010-10-17 16:28:10.729542')
    SQL (0.1ms)  INSERT INTO "ingredients_recipes" ("created_at", "description", "ingredient_id", "qty", "recipe_id", "updated_at") VALUES ('2010-10-17 16:28:10.783068', '', **0,** 62.0, 18, '2010-10-17 16:28:10.783068')
    SQL (0.1ms)  INSERT INTO "ingredients_recipes" ("created_at", "description", "ingredient_id", "qty", "recipe_id", "updated_at") VALUES ('2010-10-17 16:28:10.784333', '', **0,** 12.0, 18, '2010-10-17 16:28:10.784333')
  Redirected to http://0.0.0.0:3000/recipes/18
  Completed 302 Found in 190ms
  
  
  Started GET "/recipes/18" for 127.0.0.1 at 2010-10-17 16:28:10 +0000
    Processing by RecipesController#show as HTML
    Parameters: {"id"=&gt;"18"}
    Recipe Load (0.4ms)  SELECT "recipes".* FROM "recipes" WHERE ("recipes"."id" = 18) LIMIT 1
    IngredientsRecipe Load (0.3ms)  SELECT "ingredients_recipes".* FROM "ingredients_recipes" WHERE ("ingredients_recipes".recipe_id = 18)
    Ingredient Load (0.2ms)  SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1
    CACHE (0.0ms)  SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1
    CACHE (0.0ms)  SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1
    CACHE (0.0ms)  SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1
    CACHE (0.0ms)  SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1
    CACHE (0.0ms)  SELECT "ingredients".* FROM "ingredients" WHERE ("ingredients"."id" = 0) LIMIT 1
  Rendered recipes/show.html.erb within layouts/application (112.1ms)
  Completed 200 OK in 143ms (Views: 117.4ms | ActiveRecord: 0.9ms)
  </code></pre>
  
  <p>I have made the text bold in the example above where it's writing the text instead of the id from the autocomplete.  I don't have a clue how to set it up to use the id instead.</p>
  
  <p>Help would be very appreciated.</p>