ActiveModel::UnknownAttributeError: unknown attribute 'admin' for User

ActiveModel::UnknownAttributeError: unknown attribute 'admin' for User

我知道他们对那个错误有类似的问题,但另一个 post 对我帮助不大。过去 2 小时我一直在查看我的代码,但无法理解。

当我运行:rails db:seed

我得到了:ActiveModel::UnknownAttributeError: unknown attribute 'admin' for User.

# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
#   Character.create(name: 'Luke', movie: movies.first)
User.create!(email: 'admin@example.com', password: 'password', password_confirmation: 'password', admin: true) if Rails.env.development?


bob = User.create!(first_name: "Bob", last_name: "Gratton", title: "Clown", email: "bobgratton@bg", password: "123456", password_confirmation: "1234564", admin: true)


User.create!(first_name: 'Nicolas', last_name: 'Genest', title: 'Comm Rep', email: 'nicolas.genest@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Nadya', last_name: 'Fortier', title: 'Director', email: 'nadya.fortier@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true )
User.create!(first_name: 'Martin', last_name: 'Chantal', title: 'Director Assistant', email: 'martin.chantal@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Mathieu', last_name: 'Houde', title: 'Captain', email: 'mathieu.houde@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true)
User.create!(first_name: 'David', last_name: 'Boutin', title: 'Engineer', email: 'david.boutin@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Mathieu', last_name: 'Lortie', title: 'Engineer', email: 'mathieu.lortie@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Thomas', last_name: 'Carrier', title: 'Engineer', email: 'thomas.carrier@codeboxx.biz', password: '123456', password_confirmation: '123456', admin: true) 


require 'faker'
require 'date'

300.times do |n|
    nameF = Faker::Name.name
    businessF = Faker::Company.name
    emailF = Faker::Internet.email
    phoneF = Faker::PhoneNumber.phone_number
    projname = Faker::Lorem.words(number: 3, supplemental: true)
    descriptionF = Faker::Lorem.sentence(word_count: 4, supplemental: true, random_words_to_add: 3)
    departmentF = ["Question","Corporate","Residential"].sample
    informationF = Faker::Lorem.paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 3)
    yearF = rand(2017..2020)
    monthF = rand(1..12)
    if monthF == 2
        dayF = rand(1..28)
    else
        dayF = rand(1..30)
    end
    c = Date.new(yearF, monthF, dayF)
    c.strftime("%F")
    # fileF = ""

    Lead.create(
        full_name: nameF,
        company_name: businessF,
        email: emailF,
        phone: phoneF,
        project_name: projname,
        project_description: descriptionF,
        department: departmentF,
        message: informationF,
        # attached_file: fileF,
        created_at: c,
        updated_at: c
    )

    r = rand(3)
    
    if r == 0
        building_type = "residential"
        Product_Quality = ["Standard","Premium","Excellium"].sample
        if Product_Quality == "Standard"
            price_per_elev = 7565
            fee = 0.1
        elsif Product_Quality == "Premium"
            price_per_elev = 12345
            fee = 0.13
        else
            price_per_elev = 15400
            fee = 0.16
        end
        x = rand(50..500)
        y = rand(2..45)
        z = rand(3)

        cages = rand(1..24)

        Quote.create(
            Full_Name: nameF,
            Company_Name: businessF,
            Email: emailF,
            Building_Type: building_type,
            Product_Quality: Product_Quality,
            Nb_Appartement: x,
            Nb_Floor: y,
            Nb_Basement: z,
            Nb_Ele_Suggested: cages,
            Price_Per_Ele: price_per_elev,
            Subtotal: cages * price_per_elev,
            Install_Fee: cages * price_per_elev * fee,
            Final_Price: cages * price_per_elev * (1+fee),
            created_at: c,
            updated_at: c
        )
    elsif r == 1
        building_type = "commercial"
        Product_Quality = ["Standard","Premium","Excellium"].sample
        if Product_Quality == "Standard"
            price_per_elev = 7565
            fee = 0.1
        elsif Product_Quality == "Premium"
            price_per_elev = 12345
            fee = 0.13
        else
            price_per_elev = 15400
            fee = 0.16
        end
        x = rand(50..500)
        y = rand(2..45)
        z = rand(3)

        cages = rand(1..24)

        Quote.create(
            Full_Name: nameF,
            Company_Name: businessF,
            Email: emailF,
            Building_Type: building_type,
            Product_Quality: Product_Quality,
            Nb_Business: x,
            Nb_Floor: y,
            Nb_Basement: z,
            Nb_Cage: cages,
            Nb_Parking: x,
            Nb_Ele_Suggested: cages,
            Price_Per_Ele: price_per_elev,
            Subtotal: cages * price_per_elev,
            Install_Fee: cages * price_per_elev * fee,
            Final_Price: cages * price_per_elev * (1+fee),
            created_at: c,
            updated_at: c
        )
    elsif r == 2
        building_type = "corporate"
        Product_Quality = ["Standard", "Premium", "Excellium"].sample
        if Product_Quality == "Standard"
            price_per_elev = 7565
            fee = 0.1
        elsif Product_Quality == "Premium"
            price_per_elev = 12345
            fee = 0.13
        else
            price_per_elev = 15400
            fee = 0.16
        end
        x = rand(50..500)
        y = rand(2..45)
        z = rand(3)

        cages = rand(1..24)

        Quote.create(
            Full_Name: nameF,
            Company_Name: businessF,
            Email: emailF,
            Building_Type: building_type,
            Product_Quality: Product_Quality,
            Nb_Company: x,
            Nb_Floor: y,
            Nb_Basement: z,
            Nb_Parking: x,
            Nb_OccupantPerFloor: rand(300..500),
            Nb_Ele_Suggested: cages,
            Price_Per_Ele: price_per_elev,
            Subtotal: cages * price_per_elev,
            Install_Fee: cages * price_per_elev * fee,
            Final_Price: cages * price_per_elev * (1+fee),
            created_at: c,
            updated_at: c
        )
    else
        building_type = "hybrid"
        Product_Quality = ["Standard","Premium","Excellium"].sample
        if Product_Quality == "Standard"
            price_per_elev = 7565
            fee = 0.1
        elsif Product_Quality == "Premium"
            price_per_elev = 12345
            fee = 0.13
        else
            price_per_elev = 15400
            fee = 0.16
        end
        x = rand(50..500)
        y = rand(2..45)
        z = rand(3)

        cages = rand(1..24)

        Quote.create(
            Full_Name: nameF,
            Company_Name: businessF,
            Email: emailF,
            Building_Type: building_type,
            Product_Quality: Product_Quality,
            Nb_Business: x,
            Nb_Floor: y,
            Nb_Basement: z,
            Nb_Parking: x,
            Nb_OccupantPerFloor: rand(300..500),
            Nb_OperatingHour: rand(1..24),
            Nb_Ele_Suggested: cages,
            Price_Per_Ele: price_per_elev,
            Subtotal: cages * price_per_elev,
            Install_Fee: cages * price_per_elev * fee,
            Final_Price: cages * price_per_elev * (1+fee),
            created_at: c,
            updated_at: c
        )
    end
end

50.times do |n|

    typeF = ["Billing", "Shipping", "Home", "Business"].sample
    statusF = ["active", "inactive"].sample
    entityF = "customer"
    addrF = Faker::Address.street_address
    secondary_addressF = Faker::Address.secondary_address
    cityF = Faker::Address.city
    postalF = Faker::Address.zip
    notesF = Faker::Lorem.sentence(word_count: 4, supplemental: true, random_words_to_add: 5)

    fake_address = Address.create!(
        type_of_address: typeF,
        status: statusF,
        entity: entityF,
        number_and_street: addrF,
        suite_or_apartment: secondary_addressF,
        city: cityF,
        postal_code: postalF,
        country: "CAN",
        notes: notesF
    )

    nameF = Faker::Name.name
    unique_businessF = Faker::Company.name
    phoneF = Faker::PhoneNumber.phone_number
    emailF = Faker::Internet.email
    yearF = rand(2017..2019)
    monthF = rand(1..12)
    if monthF == 2
        dayF = rand(1..28)
    else
        dayF = rand(1..30)
    end
    c = Date.new(yearF, monthF, dayF)
    c.strftime("%F")

    fake_customer = Customer.create!(
        user_id: fake_user.id,
        customers_create_date: c,
        company_name: unique_businessF,
        address_id: fake_address.id,
        full_name_company_contact: nameF,
        company_contact_phone: phoneF,
        company_contact_email: emailF,
        company_description: notesF,
        full_name_STA: nameF,
        phone_TA: phoneF,
        email_TA: emailF,
        #created_at: c,
        #updated_at: c
    )
    additional_building = rand(1..3)
    additional_building.times do |x|
        if x > 0
            typeF = ["Billing", "Shipping", "Home", "Business"].sample
            statusF = ["active", "inactive"].sample
            entityF = "building"
            addrF = Faker::Address.street_address
            secondary_addressF = Faker::Address.secondary_address
            cityF = Faker::Address.city
            postalF = Faker::Address.zip
            notesF = Faker::Lorem.sentence(word_count: 4, supplemental: true, random_words_to_add: 5)
            
            fake_address = Address.create!(
                type_of_address: typeF,
                status: statusF,
                entity: entityF,
                number_and_street: addrF,
                suite_or_apt: secondary_addressF,
                city: cityF,
                postal_code: postalF,
                country: "CAN",
                notes: notesF
            )
        end

        fake_building = Building.create!(
            customer_id: fake_customer.id,
            address_id: fake_address.id,
            admin_full_name: nameF,
            admin_email: emailF,
            admin_phone: phoneF,
            full_name_STA: nameF,
            phone_TA: phoneF,
            email_TA: emailF,
        )

        buildingtypeF = ["Residential", "Commercial", "Corporate", "Hybrid"].sample
        BuildingDetails.create!(
            building_id: fake_building.id,
            info_key: "Type",
            value: buildingtypeF
        )


        certificateF = Faker::IDNumber.valid
        notesF = Faker::Lorem.sentence(word_count: 3, supplemental: true, random_words_to_add: 3)

        fake_batteries = Batteries.create!(
            building_id: fake_building.id,
            batteries_type: buildingtypeF,
            status: "active",
            employee_id: bob.id,
            commission_date: c,
            date_of_last_inspection: c,
            certificate_of_operations: certificateF,
            informations: notesF,
            notes: notesF,
            # created_at: c,
            # updated_at: c
        )
        floorsF = rand(2..20)
        rand(1..5).times do |n|
            fake_column = Column.create!(
                batteries_id: fake_batteries.id,
                columns_type: buildingtypeF,
                numbers_of_floors: floorsF,
                status: statusF,
                informations: notesF,
                notes: notesF
            )
            rand(1..6).times do |n|
                certificateF = Faker::IDNumber.valid,
                Elevator.create!(
                    columns_id: fake_column.id,
                    serial_number: Faker::IDNumber.south_african_id_number,
                    model: ["Standard", "Premium", "Excelium"].sample,
                    elevators_type: buildingtypeF,
                    status: "active",
                    commission_date: c,
                    date_of_last_inspection: c,
                    certificate_of_inspection: certificateF,
                    informations: notesF,
                    notes: notesF,
                    # created_at: c,
                    # created_at: c
                )
            end
        end
    end
end 

这是我的架构:

# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_14_152431) do

 create_table "active_admin_comments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "namespace"
   t.text "body"
   t.string "resource_type"
   t.bigint "resource_id"
   t.string "author_type"
   t.bigint "author_id"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
   t.index ["namespace"], name: "index_active_admin_comments_on_namespace"
   t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
 end

 create_table "addresses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "type_of_address", null: false
   t.string "status", null: false
   t.string "entity", null: false
   t.string "number_and_street", null: false
   t.string "suite_or_apartment", default: ""
   t.string "city", null: false
   t.string "postal_code", null: false
   t.string "country", null: false
   t.text "notes"
 end

 create_table "batteries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "batteries_type", null: false
   t.string "status", null: false
   t.date "commission_date", null: false
   t.date "date_of_last_inspect", null: false
   t.binary "certificate_of_operations", null: false
   t.string "informations"
   t.string "notes"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end

 create_table "building_details", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "info_key", null: false
   t.string "value", null: false
 end

 create_table "buildings", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "admin_full_name", null: false
   t.string "admin_phone", null: false
   t.string "admin_email", null: false
   t.string "full_name_STA"
   t.string "phone_TA"
   t.string "email_TA"
 end

 create_table "columns", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "columns_type", null: false
   t.integer "number_of_floors", null: false
   t.string "status", null: false
   t.text "informations"
   t.text "notes"
 end

 create_table "customers", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "company_name", null: false
   t.string "full_name_company_contact", null: false
   t.string "company_contact_phone", null: false
   t.string "company_contact_email", null: false
   t.text "company_description"
   t.string "full_name_STA", null: false
   t.string "phone_TA", null: false
   t.string "email_TA", null: false
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end

 create_table "elevators", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.bigint "serial_number", null: false
   t.string "model", null: false
   t.string "elevators_type", null: false
   t.string "status", null: false
   t.date "commission_date", null: false
   t.date "date_of_last_inspection", null: false
   t.string "certificate_of_inspection"
   t.text "informations"
   t.text "notes"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end

 create_table "employees", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "email", default: "", null: false
   t.string "encrypted_password", default: "", null: false
   t.bigint "user_id"
   t.string "reset_password_token"
   t.datetime "reset_password_sent_at"
   t.string "first_name"
   t.string "last_name"
   t.string "title"
   t.datetime "remember_created_at"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.boolean "admin", default: false
   t.index ["email"], name: "index_employees_on_email", unique: true
   t.index ["reset_password_token"], name: "index_employees_on_reset_password_token", unique: true
   t.index ["user_id"], name: "index_employees_on_user_id"
 end

 create_table "leads", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "Full_Name"
   t.string "Compagny_Name"
   t.string "Email"
   t.string "Phone"
   t.string "Project_Name"
   t.text "Project_Description"
   t.string "Department"
   t.text "Message"
   t.binary "File_Data", limit: 16777215
   t.string "File_name"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end

 create_table "quotes", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "Full_Name"
   t.string "Phone_Number"
   t.string "Company_Name"
   t.string "Email"
   t.string "Building_Type"
   t.string "Product_Quality"
   t.integer "Nb_Appartement"
   t.integer "Nb_Business"
   t.integer "Nb_Company"
   t.integer "Nb_Floor"
   t.integer "Nb_Basement"
   t.integer "Nb_Cage"
   t.integer "Nb_Parking"
   t.integer "Nb_OccupantPerFloor"
   t.string "Nb_OperatingHour"
   t.string "Nb_Ele_Suggested"
   t.string "Price_Per_Ele"
   t.string "Subtotal"
   t.string "Install_Fee"
   t.string "Final_Price"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end

 create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
   t.string "email", default: "", null: false
   t.string "encrypted_password", default: "", null: false
   t.string "reset_password_token"
   t.datetime "reset_password_sent_at"
   t.string "first_name"
   t.string "last_name"
   t.string "title"
   t.datetime "remember_created_at"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
   t.index ["email"], name: "index_users_on_email", unique: true
   t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
 end

 add_foreign_key "employees", "users"
end

您似乎没有 admin 用户栏。您可以通过 运行 执行以下操作来创建一个:

rails generate migration AddAdminToUsers admin:boolean

然后您可以运行rails db:migrate应用迁移。

似乎唯一具有管理员标志的 table 是员工 table。也许您打算创建员工而不是用户?

以后当你post一个问题时,尽量提供最少的代码来重现它。

例如,似乎只有 seeds.rb 文件的第一行引起了问题。如果您只将其提供给我们,那么帮助您会容易得多。

添加 schema.rb 很有帮助,但由于这是特定于用户的 table 我不需要了解其他 table 的所有详细信息