私はLYNDA.COMのKevin Skoglundチュートリアルに従っています。私は問題を抱えていたときリッチ」のビデオ私が走った最後の行(section.section_edits
) は、次の結果:私は「多対多」団体に沿って従っていました。上のsection_id:nilセクション番号:1
SectionEdit id: 6, admin_user_id: 1, section_id: nil, ...
しかしケビンの結果
明らかSectionEdit id: 1, admin_user_id: 1, section_id: 1, ...
が、私は私のSectionEdit id:
は彼と異なっている理由である私のエラーを見つけることを試みるために戻って何度か行きましたしているが、私の質問です::チュートリアルだった。ある理由私のsection_id: nil
EDIT:09/25/16
私はコマンドプロンプトに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}]}>
は私のコンソルと関連クラスです。ありがとう!
MYコマンドプロンプト
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]
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")
端 端
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]
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']
エンド エンド
モデル:ページ。RB
`クラスのページ< ApplicationRecord
belongs_to :subject
has_many :sections
has_and_belongs_to_many :editors, :class_name => "AdminUser"
エンド `
コントローラー:ページ
class PagesController < ApplicationController end
移行:ページ
`クラスCreatePages <のActiveRecord ::マイグレーション[5.0] DEF CREATE_TABLE変更:ページが行う|トンを|明らかにここに、あなたのコンソールをチェック
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`