title: Rails 3 ActiveRecord has_many polymorphic - Why isn't the id of the first object being saved for insertion into the join table?
body: <p>Wise people,</p>
  
  <p>Please help me troubleshoot my has_many polymorphic object creation problem.</p>
  
  <p>I have a polymorphic has_many relationship between two objects with a join table in the middle.  Acronyms (and other objects) on one side, Categories on the other.  In between them, I have a join object between the two objects.</p>
  
  <p>I cannot successfully create an acronym with a category.  I can, however, create an acronym without a category, then add the category to the existing acronym.</p>
  
  <p>My model objects look like the following code:</p>
  
  <pre><code>class Acronym &lt; ActiveRecord::Base
    has_many :category_belongings, :as =&gt; :categorizable, :dependent =&gt; :delete_all
    has_many :categories, :through =&gt; :category_belongings
  end
  
  class Category &lt; ActiveRecord::Base
    has_many :category_belongings, :dependent =&gt; :delete_all
    has_many :acronyms, :through =&gt; :category_belongings, :source =&gt; :categorizable, :source_type =&gt; 'Acronym'
  end
  
  class CategoryBelonging &lt; ActiveRecord::Base
    belongs_to :categorizable, :polymorphic =&gt; true
    belongs_to :category
  end
  </code></pre>
  
  <p>For reference, I removed some extra fields and irrelevant rules.  I also refactored my code to look just like the example here:
  <a href="https://stackoverflow.com/questions/5886738/setting-up-a-polymorphic-has-many-through-relationship/6011770#6011770">Setting up a polymorphic has_many :through relationship</a></p>
  
  <p>Through my Rails application/rails console, I am able to:</p>
  
  <ul>
  <li>Successfully create an acronym without any categories</li>
  <li>Successfully create a category</li>
  <li>Successfully add an existing category to an existing acronym through updating the acronym</li>
  </ul>
  
  <p>I cannot, however, create a new acronym with an existing category.  I can see in the logs Rails tries to do the following:</p>
  
  <ol>
  <li>Gathers the parameters from the post.  These parameters include the columns on the Acronym table as well as the category IDs</li>
  <li>Checks to see if the Category exists.  It does.</li>
  <li>Checks to see if the Acronym exists.  It does not.</li>
  <li>Inserts a new Acronym into the database with all the column parameters</li>
  <li>Attempts to insert a new entry in the join table.  This is where the error is.  The SQL for this insertion contains a Nil value for the Acronym ID.</li>
  <li><p>Rolls my changes back.</p>
  
  <pre><code>Processing by Admin::AcronymsController#create as HTML
    Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"/uquz5FvtMh0QWP5NoWwTO9FMMEC9rsMTrTj4WUNxxE=", "acronym"=&gt;{"name"=&gt;"A Test Acronym", "definition"=&gt;"A Test Definition", "explanation"=&gt;"", "category_ids"=&gt;["1", ""], "state"=&gt;"unapproved"}, "commit"=&gt;"Create Acronym"}
    Category Load (0.4ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 LIMIT 1  [["id", 1]]
     (0.3ms)  BEGIN
    Category Exists (0.6ms)  SELECT 1 AS one FROM "categories" WHERE ("categories"."name" = 'Internet' AND "categories"."id" != 1) LIMIT 1
    Acronym Exists (26.0ms)  SELECT 1 AS one FROM "acronyms" WHERE "acronyms"."name" = 'A Test Acronym' LIMIT 1
    SQL (18.1ms)  INSERT INTO "acronyms" ("definition", "explanation", "improvement_reason", "likes_count", "name", "state", "submitter_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["definition", "A Test Definition"], ["explanation", ""], ["improvement_reason", nil], ["likes_count", 0], ["name", "A Test Acronym"], ["state", "unapproved"], ["submitter_id", nil]]
    SQL (55.8ms)  INSERT INTO "category_belongings" ("categorizable_id", "categorizable_type", "category_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["categorizable_id", nil], ["categorizable_type", "Acronym"], ["category_id", 1], ["created_at", Thu, 28 Feb 2013 23:03:48 UTC +00:00], ["updated_at", Thu, 28 Feb 2013 23:03:48 UTC +00:00]]
  PG::Error: ERROR:  null value in column "categorizable_id" violates not-null constraint
  : INSERT INTO "category_belongings" ("categorizable_id", "categorizable_type", "category_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"
     (0.3ms)  ROLLBACK
  </code></pre></li>
  </ol>
  
  <p>I would assume on the Acronym insert, an acronym_id would be created, but I don't see any output indicating what the value of that acronym_id is.  To me, the error is clearly that the acronym_id is Nil when I try to insert the new row into category_belongings.  Do I need to somehow point acronym_id to categorizable_id?  If that's the case, how would I do this?</p>
  
  <p>I'm new to rails, and I am trying to maintain and improve an existing system.  Thanks for any help.</p>