section_id:无 当它应该是 section_id:1

section_id: nil WHEN IT SHOULD BE section_id: 1

我正在关注 Kevin Skoglund 的 LYNDA.COM 教程。当我遇到问题时,我正在关注 "Many-to-many" 协会:Rich”视频。最后一行我 运行 (section.section_edits) 结果如下:

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

但是 Kevin 在教程中的结果是:

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

显然我已经回过头几次试图找出我的错误,这就是为什么我的 SectionEdit id: 与他的不同。但我的问题是:为什么我的 section_id: nil

编辑:2016 年 9 月 25 日

我在命令提示符中输入了 section.errors,但不完全理解控制台的响应。 @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}]}>

下面是我的控制台和关联的 classes。谢谢!

我的命令提示符

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"

结束

部分迁移

class CreateSections < ActiveRecord::Migration[5.0]

默认更改 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")

结束 结束

section_edit.rb

class SectionEdit < ApplicationRecord

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

结束

section_edits迁移

class CreateSectionEdits < ActiveRecord::Migration[5.0]

默认更改 create_table :section_edits 做 |t|

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

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

结束 结束

型号:page.rb

`class 页面 < ApplicationRecord

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

结束 `

控制器:页面

class PagesController < ApplicationController end

迁移:页面

`class 创建页面 < ActiveRecord::Migration[5.0] 定义更改 create_table :页数 |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")

结束 结束`

检查你的控制台,显然这里有问题:

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>

如您所见,创建部分后由于验证错误而回滚,因此部分对象具有 id: nil。我想这是因为您没有在问题代码中包含验证,但在创建该部分后,您可以使用 section.errors 轻松检查控制台中的错误并查看问题所在。

只是想添加一些信息以防其他人遇到同样的问题。

A​​lter Lagos 是正确的,该部分有一个限制,它必须分配一个页面。这就是导致调用失败的原因。

要创建部分对象,请执行以下操作:(它不在视频中)。

page = Page.first

然后创建栏目并传入页面。

section = Section.create(:name => "Section One", :position => 1, :page => page)

在 section.rb 文件中 - 做了 belongs_to:page, {:optional => true}

然后在 rails 控制台 运行- section = Section.create(:name => "Section One", :position => 1)

干杯