0
votes

I know their were similar questions about that error, but the other post didn't help me much. I've been looking at my code for the past 2 hours and can't figure it.

When I run: rails db:seed

I got: 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: '[email protected]', 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: '[email protected]', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Nadya', last_name: 'Fortier', title: 'Director', email: '[email protected]', password: '123456', password_confirmation: '123456', admin: true )
User.create!(first_name: 'Martin', last_name: 'Chantal', title: 'Director Assistant', email: '[email protected]', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Mathieu', last_name: 'Houde', title: 'Captain', email: '[email protected]', password: '123456', password_confirmation: '123456', admin: true)
User.create!(first_name: 'David', last_name: 'Boutin', title: 'Engineer', email: '[email protected]', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Mathieu', last_name: 'Lortie', title: 'Engineer', email: '[email protected]', password: '123456', password_confirmation: '123456', admin: true) 
User.create!(first_name: 'Thomas', last_name: 'Carrier', title: 'Engineer', email: '[email protected]', 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 

Here is my schema:

# 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
1

1 Answers

2
votes

It doesn't look like you have an admin column for the users. You can create one by running the following:

rails generate migration AddAdminToUsers admin:boolean

Then you can run rails db:migrate to apply the migration.

It seems like the only table which has an admin flag is the employees table. Perhaps you meant to create an employee and not a user?

In the future when you post a question try to supply the minimum amount of code to reproduce it.

For example it seems to be only the first line of the seeds.rb file which is causing an issue. If you had only provided that to us it makes helping you a lot easier.

Adding the schema.rb was helpful, but again since this is specific to the users table I didn't need to know all the details about the other tables