Search code examples
many-to-manyruby-on-rails-5model-associations

section_id: nil WHEN IT SHOULD BE section_id: 1


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 (section.section_edits) resulted in the following:

SectionEdit id: 6, admin_user_id: 1, section_id: nil,  ...

However Kevin's result on the tutorial was:

SectionEdit id: 1, admin_user_id: 1, section_id: 1,  ...

Obviously I have went back several times to try to find my error which is why my SectionEdit id: is different from his. BUT MY QUESTION IS: why is my section_id: nil

EDIT: 09/25/16

I entered section.errors in my command prompts but don't fully understand the console response. @AlterLagos

irb(main):004:0> section = Section.create(:name => "Section One", :position => 1) (0.5ms) BEGIN (1.0ms) ROLLBACK => #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil> irb(main):005:0> section.errors => #<ActiveModel::Errors:0x6340fe0 @base=#<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil>, @messages={:page=>["must exist"]}, @details={:page=>[{:error=>:blank}]}>

Below is my consol and associated classes. THANKS!

MY COMMAND PROMPT

C:\Users\David\My Documents\sites\simple_cms>rails console
Loading development environment (Rails 5.0.0.1)

irb(main):001:0> me = AdminUser.find(1)
  AdminUser Load (4.0ms)  SELECT  `admin_users`.* FROM `admin_users` WHERE`admin_users`.`id` = 1 LIMIT 1
=> #<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>

irb(main):002:0> me.section_edits
  SectionEdit Load (3.5ms)  SELECT `section_edits`.* FROM `section_edits` WHERE `section_edits`.`admin_user_id` = 1
=> #<ActiveRecord::Associations::CollectionProxy [#<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">, #<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">, #<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">, #<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">, #<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">]>

irb(main):003:0> section = Section.create(:name => "Section One", :position => 1)
   (1.0ms)  BEGIN
   (1.0ms)  ROLLBACK
=> #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil>

irb(main):004:0> section.section_edits => #<ActiveRecord::Associations::CollectionProxy []>

irb(main):005:0> edit = SectionEdit.new
=> #<SectionEdit id: nil, admin_user_id: nil, section_id: nil, summary: nil, created_at: nil, updated_at: nil>

irb(main):006:0> edit.summary = "Test edit"
=> "Test edit"

irb(main):007:0> section.section_edits << edit
=> #<ActiveRecord::Associations::CollectionProxy [#<SectionEdit id: nil, admin_user_id: nil, section_id: nil, summary: "Test edit", created_at: nil, updated_at: nil>]>

irb(main):008:0> edit.editor = me
=> #<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>

irb(main):009:0> 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
=> true

irb(main):010:0> section.section_edits
=> #<ActiveRecord::Associations::CollectionProxy [#<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">]>

irb(main):011:0>

section.rb

class Section < ApplicationRecord

belongs_to :page
has_many :section_edits
has_many :editors, :through => :section_edits, :class_name => "AdminUser"

end

Sections Migration

class CreateSections < ActiveRecord::Migration[5.0]

def change create_table :sections do |t|

  t.integer "page_id"
  # same as t.reference :page
  t.string "name"
  t.integer "position"
  t.boolean "visible", :default => false
  t.string "content_type"
  t.text "content"
  t.timestamps

end

add_index("sections", "page_id")

end end

section_edit.rb

class SectionEdit < ApplicationRecord

belongs_to :editor, :class_name => "AdminUser", :foreign_key => 'admin_user_id'
belongs_to :section

end

section_edits migration

class CreateSectionEdits < ActiveRecord::Migration[5.0]

def change create_table :section_edits do |t|

t.references :admin_user
t.references :section
t.string "summary"
t.timestamps

end
add_index :section_edits, ['admin_user_id', 'section_id']

end end

Model: page.rb

`class Page < ApplicationRecord

belongs_to :subject
has_many :sections
has_and_belongs_to_many :editors, :class_name => "AdminUser"

end `

Controller: Pages

class PagesController < ApplicationController end

Migration: Page

`class CreatePages < ActiveRecord::Migration[5.0] def change create_table :pages do |t|

  t.integer "subject_id"
  # same as t.references :subject
  t.string "name"
  t.string "permalink"
  t.integer "position"
  t.boolean "visible", :default => false
  t.timestamps

end
add_index("pages", "subject_id")
add_index("pages", "permalink")

end end`


Solution

  • Checking your console, clearly here is a problem:

    irb(main):003:0> section = Section.create(:name => "Section One", :position => 1)
       (1.0ms)  BEGIN
       (1.0ms)  ROLLBACK
    => #<Section id: nil, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: nil, updated_at: nil>
    

    As you can see, is rolling back because of a validation error after to create the section, so the section object has an id: nil. I guess this is because a validation you didn't include in your question code, but after create the section, you could check easily the error in the console with section.errors and see what is the problem.