Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing build with has_many :through
    text
    copied!<p>I have an <code>Entry</code> model and a <code>Category</code> model, where an Entry can have many Categories (through <code>EntryCategories</code>):</p> <pre><code>class Entry &lt; ActiveRecord::Base belongs_to :journal has_many :entry_categories has_many :categories, :through =&gt; :entry_categories end class Category &lt; ActiveRecord::Base has_many :entry_categories, :dependent =&gt; :destroy has_many :entries, :through =&gt; :entry_categories end class EntryCategory &lt; ActiveRecord::Base belongs_to :category belongs_to :entry end </code></pre> <p>When creating a new Entry, I create it by calling <code>@journal.entries.build(entry_params)</code>, where <code>entry_params</code> is the parameters from the entry form. If any categories are selected, however, I get this error:</p> <pre><code>ActiveRecord::HasManyThroughCantDissociateNewRecords in Admin/entriesController#create Cannot dissociate new records through 'Entry#entry_categories' on '#'. Both records must have an id in order to delete the has_many :through record associating them. </code></pre> <p>Note that the '#' on the second line is verbatim; it doesn't output an object.</p> <p>I have tried naming my categories selectbox on the form to <code>categories</code> and <code>category_ids</code> but neither make a difference; if either is in the <code>entry_params</code>, the save will fail. If no categories are selected, or I remove <code>categories</code> from <code>entry_params</code> (<code>@entry_attrs.delete(:category_ids)</code>), the save works properly, but the categories don't save, obviously.</p> <p>It seems to me that the problem is that an EntryCategory record is attempting to be made before the Entry record is saved? Shouldn't build be taking care of that?</p> <p><strong>Update:</strong></p> <p>Here's the relevant parts of schema.rb, as requested:</p> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20090516204736) do create_table "categories", :force =&gt; true do |t| t.integer "journal_id", :null =&gt; false t.string "name", :limit =&gt; 200, :null =&gt; false t.integer "parent_id" t.integer "lft" t.integer "rgt" end add_index "categories", ["journal_id", "parent_id", "name"], :name =&gt; "index_categories_on_journal_id_and_parent_id_and_name", :unique =&gt; true create_table "entries", :force =&gt; true do |t| t.integer "journal_id", :null =&gt; false t.string "title", :null =&gt; false t.string "permaname", :limit =&gt; 60, :null =&gt; false t.text "raw_body", :limit =&gt; 2147483647 t.datetime "created_at", :null =&gt; false t.datetime "posted_at" t.datetime "updated_at", :null =&gt; false end create_table "entry_categories", :force =&gt; true do |t| t.integer "entry_id", :null =&gt; false t.integer "category_id", :null =&gt; false end add_index "entry_categories", ["entry_id", "category_id"], :name =&gt; "index_entry_categories_on_entry_id_and_category_id", :unique =&gt; true end </code></pre> <p>Also, saving an entry with categories works fine in the update action (by calling <code>@entry.attributes = entry_params</code>), so it does seem to me that the problem is only based on the Entry not existing at the point that the EntryCategory records are attempted to be created.</p>
 

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