未知属性错误 Rails 5.1.1 - 无法通过连接识别模型属性 table

Unkown Attribute Error Rails 5.1.1 - not recognizing model attribute with join table

我将展示我的文件、我的过程,然后是这个 post 中的错误。

问题 Rails 控制台在连接时无法识别我的模型属性 table。

(如果您在阅读下文后想出更好的方式来描述 problem/or 问这个问题,请在您的回复中告诉我,因为这将帮助我了解我想问的是什么对于。)

文件

models/admin_user.rb

class AdminUser < ApplicationRecord
    has_and_belongs_to_many :pages
    has_many :section_edits
end

models/section.rb

class Section < ApplicationRecord

    belongs_to :page
    has_many :section_edits

end

models/section_edit.rb

class SectionEdit < ApplicationRecord

    belongs_to :sections
    belongs_to :admin_users

end

table 来自 schema.rb

create_table "admin_users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.string "first_name", limit: 25
    t.string "last_name", limit: 50
    t.string "email", limit: 100, default: "", null: false
    t.string "username", limit: 25
    t.string "hashed_password", limit: 40
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["username"], name: "index_admin_users_on_username"
  end

create_table "section_edits", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.integer "admin_user_id"
    t.integer "section_id"
    t.string "summary"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["section_id", "admin_user_id"], name: "index_section_edits_on_section_id_and_admin_user_id"
end

create_table "sections", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
    t.integer "page_id"
    t.string "name"
    t.integer "position"
    t.boolean "visible", default: false
    t.string "content_type"
    t.text "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["page_id"], name: "index_sections_on_page_id"
end

进程

  1. 我在 cloud9 IDE.
  2. 上使用 rails -c 启动 rails 控制台
  3. 我创建了一个 AdminUser 并分配给变量 "me"。 下面是变量 "me":

    的 return 值

    me

    => <#AdminUser id: 1, first_name: "john", last_name: "smith", email:"", username: "jsmith", hashed_password: nil, created_at: "2017-06-17 14:00:44", updated_at: "2017-06-17 14:00:44"> 
    
  4. 我创建一个 Section 并赋值给一个变量 "section" 下面是 "section" 的 return 值:

    section

    => <#Section id: 1, page_id: nil, name: "Section One", position: 1, visible: false, content_type: nil, content: nil, created_at: "2017-06-17 14:20:08", updated_at: "2017-06-17 14:20:08">
    
  5. 我尝试创建一个新的 SectionEdit,如下所示:

    edit = SectionEdit.new(:summary => 'Test Edit', :admin_user => me, :section => section)

错误

rails 控制台向我返回此错误:

ActiveModel::UnknownAttributeError: unknown attribute 'admin_user' for SectionEdit.
    from (irb):24

我在观看和跟随 lynda.com 的 rails 5 教程时执行了所有这些操作。这很烦人,因为讲师能够完成实例创建并将其保存到数据库中。我在 rails 5 -> 5.1.1 中错过了什么吗?非常感谢对此的任何帮助。

尝试在 SectionEdit 模型中使用单数名称:

class SectionEdit < ApplicationRecord
    belongs_to :section
    belongs_to :admin_user
end