title: section_id: nil WHEN IT SHOULD BE section_id: 1
body: <p>I'm following a LYNDA.COM tutorial with Kevin Skoglund. I was following along in the "Many-to-many" associations: Rich" video when I had a problem. The last line I ran (<code>section.section_edits</code>)
  resulted in the following:</p>
  
  <pre><code>SectionEdit id: 6, admin_user_id: 1, section_id: nil,  ...
  </code></pre>
  
  <p>However Kevin's result on the tutorial was:</p>
  
  <pre><code>SectionEdit id: 1, admin_user_id: 1, section_id: 1,  ...
  </code></pre>
  
  <p>Obviously I have went back several times to try to find my error which is why my <code>SectionEdit id:</code> is different from his. BUT MY QUESTION IS: why is my <code>section_id: nil</code></p>
  
  <p>EDIT: 09/25/16</p>
  
  <p>I entered <code>section.errors</code> in my command prompts but don't fully understand the console response. @AlterLagos</p>
  
  <p><code>irb(main):004:0&gt; section = Section.create(:name =&gt; "Section One", :position =&gt; 1)
     (0.5ms)  BEGIN
     (1.0ms)  ROLLBACK
  =&gt; #&lt;Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil&gt;
  irb(main):005:0&gt; section.errors
  =&gt; #&lt;ActiveModel::Errors:0x6340fe0 @base=#&lt;Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil&gt;, @messages={:page=&gt;["must exist"]}, @details={:page=&gt;[{:error=&gt;:blank}]}&gt;</code></p>
  
  <p>Below is my consol and associated classes. THANKS!</p>
  
  <p><strong>MY COMMAND PROMPT</strong></p>
  
  <pre><code>C:\Users\David\My Documents\sites\simple_cms&gt;rails console
  Loading development environment (Rails 5.0.0.1)
  
  irb(main):001:0&gt; me = AdminUser.find(1)
    AdminUser Load (4.0ms)  SELECT  `admin_users`.* FROM `admin_users` WHERE`admin_users`.`id` = 1 LIMIT 1
  =&gt; #&lt;AdminUser id: 1, first_name: "David", last_name: "Boyette", email: "", hashed_password: nil, created_at: "2016-09-08 02:52:57", updated_at: "2016-09-08 02:52:57", username: "dboyette", salt: nil&gt;
  
  irb(main):002:0&gt; me.section_edits
    SectionEdit Load (3.5ms)  SELECT `section_edits`.* FROM `section_edits` WHERE `section_edits`.`admin_user_id` = 1
  =&gt; #&lt;ActiveRecord::Associations::CollectionProxy [#&lt;SectionEdit id: 1, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-09 01:19:08", updated_at: "2016-09-09 01:19:08"&gt;, #&lt;SectionEdit id: 2, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-10 00:05:56", updated_at: "2016-09-10 00:05:56"&gt;, #&lt;SectionEdit id: 3, admin_user_id: 1, section_id: nil, summary: "Ch-ch-ch-changes", created_at: "2016-09-10 00:09:39", updated_at: "2016-09-10 00:09:39"&gt;, #&lt;SectionEdit id: 4, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-17 03:32:13", updated_at: "2016-09-17 03:32:13"&gt;, #&lt;SectionEdit id: 5, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-18 00:48:40", updated_at: "2016-09-18 00:48:40"&gt;]&gt;
  
  irb(main):003:0&gt; section = Section.create(:name =&gt; "Section One", :position =&gt; 1)
     (1.0ms)  BEGIN
     (1.0ms)  ROLLBACK
  =&gt; #&lt;Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil&gt;
  
  irb(main):004:0&gt; section.section_edits =&gt; #&lt;ActiveRecord::Associations::CollectionProxy []&gt;
  
  irb(main):005:0&gt; edit = SectionEdit.new
  =&gt; #&lt;SectionEdit id: nil, admin_user_id: nil, section_id: nil, summary: nil, created_at: nil, updated_at: nil&gt;
  
  irb(main):006:0&gt; edit.summary = "Test edit"
  =&gt; "Test edit"
  
  irb(main):007:0&gt; section.section_edits &lt;&lt; edit
  =&gt; #&lt;ActiveRecord::Associations::CollectionProxy [#&lt;SectionEdit id: nil, admin_user_id: nil, section_id: nil, summary: "Test edit", created_at: nil, updated_at: nil&gt;]&gt;
  
  irb(main):008:0&gt; edit.editor = me
  =&gt; #&lt;AdminUser id: 1, first_name: "David", last_name: "Boyette", email: "", hashed_password: nil, created_at: "2016-09-08 02:52:57", updated_at: "2016-09-08 02:52:57", username: "dboyette", salt: nil&gt;
  
  irb(main):009:0&gt; edit.save
     (1.5ms)  BEGIN
    SQL (3.0ms)  INSERT INTO `section_edits` (`admin_user_id`, `summary`, `created_at`, `updated_at`) VALUES (1, 'Test edit', '2016-09-18 01:44:41', '2016-09-18 01:44:41')
     (69.3ms)  COMMIT
  =&gt; true
  
  irb(main):010:0&gt; section.section_edits
  =&gt; #&lt;ActiveRecord::Associations::CollectionProxy [#&lt;SectionEdit id: 6, admin_user_id: 1, section_id: nil, summary: "Test edit", created_at: "2016-09-18 01:44:41", updated_at: "2016-09-18 01:44:41"&gt;]&gt;
  
  irb(main):011:0&gt;
  </code></pre>
  
  <p><strong>section.rb</strong></p>
  
  <pre><code>class Section &lt; ApplicationRecord
  
  belongs_to :page
  has_many :section_edits
  has_many :editors, :through =&gt; :section_edits, :class_name =&gt; "AdminUser"
  </code></pre>
  
  <p>end</p>
  
  <p><strong>Sections Migration</strong></p>
  
  <pre><code>class CreateSections &lt; ActiveRecord::Migration[5.0]
  </code></pre>
  
  <p>def change
      create_table :sections do |t|</p>
  
  <pre><code>  t.integer "page_id"
    # same as t.reference :page
    t.string "name"
    t.integer "position"
    t.boolean "visible", :default =&gt; false
    t.string "content_type"
    t.text "content"
    t.timestamps
  
  end
  
  add_index("sections", "page_id")
  </code></pre>
  
  <p>end
  end</p>
  
  <p><strong>section_edit.rb</strong></p>
  
  <pre><code>class SectionEdit &lt; ApplicationRecord
  
  belongs_to :editor, :class_name =&gt; "AdminUser", :foreign_key =&gt; 'admin_user_id'
  belongs_to :section
  </code></pre>
  
  <p>end</p>
  
  <p><strong>section_edits migration</strong></p>
  
  <pre><code>class CreateSectionEdits &lt; ActiveRecord::Migration[5.0]
  </code></pre>
  
  <p>def change
      create_table :section_edits do |t|</p>
  
  <pre><code>t.references :admin_user
  t.references :section
  t.string "summary"
  t.timestamps
  
  end
  add_index :section_edits, ['admin_user_id', 'section_id']
  </code></pre>
  
  <p>end
  end</p>
  
  <p><strong>Model: page.rb</strong></p>
  
  <p>`class Page &lt; ApplicationRecord</p>
  
  <pre><code>belongs_to :subject
  has_many :sections
  has_and_belongs_to_many :editors, :class_name =&gt; "AdminUser"
  </code></pre>
  
  <p>end
  `</p>
  
  <p><strong>Controller: Pages</strong></p>
  
  <p><code>class PagesController &lt; ApplicationController
  end
  </code></p>
  
  <p><strong>Migration: Page</strong></p>
  
  <p>`class CreatePages &lt; ActiveRecord::Migration[5.0]
    def change
      create_table :pages do |t|</p>
  
  <pre><code>  t.integer "subject_id"
    # same as t.references :subject
    t.string "name"
    t.string "permalink"
    t.integer "position"
    t.boolean "visible", :default =&gt; false
    t.timestamps
  
  end
  add_index("pages", "subject_id")
  add_index("pages", "permalink")
  </code></pre>
  
  <p>end
  end`</p>