ruby-on-rails模型无法识别预加载?

carvr3hs  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(252)

我正在尝试连接mariadb数据库中的两个表。大厅的table和宿舍的table。目标是显示每个大厅的所有经理姓名以及他们在哪个大厅工作。当我运行应用程序时,我得到以下错误:霍尔(hallid:integer,hallname:text,street:text,city:text,postcode:integer,phonenumber:integer,sfaffnumber:integer)未被识别为预加载
我不知道这是什么意思。
霍尔模型

class Hall < ApplicationRecord
  has_many :ResidenceStaffs
end

员工模型

class ResidenceStaff < ApplicationRecord
  belongs_to :Hall

      class << self

        def managers
          where(position: 'manager')
        end
      end
end

控制器

class HomeController < ApplicationController

  def index
    @residence_staff = ResidenceStaff.all
    @residence_staff_managers = ResidenceStaff.managers.includes(Hall)
  end
end

看法

<h1>Title</h1>

<div>
  <% @residence_staff_managers.each do |residence_staff| %>
    <%= residence_staff.fName%>
    <%= residence_staff.hallName%>
  <% end %>
</div>

我有一种感觉,这是一个与数据库mygrations的问题,但我不完全确定。也,
架构.rb

ActiveRecord::Schema.define(version: 20180426051412) do

  create_table "advisor", primary_key: "advisorNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "fName", null: false
    t.text "lName", null: false
    t.text "position", null: false
    t.text "deptName", null: false
    t.integer "phoneNumber", null: false
    t.string "email", limit: 100, null: false
    t.integer "roomNumber", null: false
    t.index ["advisorNumber"], name: "Advisor_advisorNumber_uindex", unique: true
    t.index ["email"], name: "Advisor_email_uindex", unique: true
  end

  create_table "hall_of_residences", primary_key: "hallName", id: :string, limit: 50, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string "street", limit: 50, null: false
    t.text "city", null: false
    t.integer "postcode", null: false
    t.integer "phoneNumber", null: false
    t.text "hall", null: false
    t.integer "staffNumber", null: false
    t.index ["hallName"], name: "HallOfResidence_hallName_uindex", unique: true
    t.index ["staffNumber"], name: "HallOfResidence_ResidenceStaff_staffNumber_fk"
  end

  create_table "halls", primary_key: "hallId", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "hallName", limit: 255, null: false
    t.text "street", null: false
    t.text "city", null: false
    t.integer "postcode", null: false
    t.integer "phoneNumber", null: false
    t.integer "sfaffNumber", null: false
    t.index ["sfaffNumber"], name: "halls_residence_staffs_staffNumber_fk"
  end

  create_table "invoice", primary_key: "invoiceNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string "semester", limit: 50
    t.date "paymentDue"
    t.text "studentFName", null: false
    t.text "studentLName", null: false
    t.string "roomLocation", limit: 50, null: false
    t.text "city", null: false
    t.string "street", limit: 50, null: false
    t.integer "postcode", null: false
    t.integer "leaseNumber", null: false
    t.index ["invoiceNumber"], name: "Invoice_invoiceNumber_uindex", unique: true
    t.index ["leaseNumber"], name: "Invoice_Lease_leaseNumber_fk"
  end

  create_table "invoice_payment", primary_key: "paymentID", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.date "datePayed", null: false
    t.string "method", limit: 50, null: false
    t.date "dateOfFirstReminder"
    t.date "dateOfSecondReminder"
    t.integer "muNum", null: false
    t.index ["muNum"], name: "InvoicePayment_Student_mUNumber_fk"
    t.index ["paymentID"], name: "InvoicePayment_paymentID_uindex", unique: true
  end

  create_table "lease", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.integer "leaseNumber", null: false
    t.string "duration", limit: 50, null: false
    t.text "studentFName", null: false
    t.text "studentLName", null: false
    t.integer "placeNumber", null: false
    t.integer "roomNumber", null: false
    t.text "city", null: false
    t.string "street", limit: 50, null: false
    t.integer "postcode", null: false
    t.date "dateToLeave", null: false
    t.date "dateToEnter", null: false
    t.integer "mUNum", null: false
    t.index ["leaseNumber"], name: "Lease_leaseNumber_uindex", unique: true
    t.index ["mUNum"], name: "Lease_Student_mUNumber_fk"
    t.index ["placeNumber"], name: "Lease_placeNumber_uindex", unique: true
  end

  create_table "next_of_kin", primary_key: "nextID", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "fName", null: false
    t.text "lName", null: false
    t.text "relationship", null: false
    t.text "city", null: false
    t.string "street", limit: 100, null: false
    t.integer "postcode", null: false
    t.integer "phoneNumber", null: false
    t.integer "mUNum", null: false
    t.index ["mUNum"], name: "NextOfKin_Student_mUNumber_fk"
    t.index ["nextID"], name: "NextOfKin_nextID_uindex", unique: true
  end

  create_table "parking_lot", primary_key: "lotNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "lotName", null: false
    t.string "street", limit: 100, null: false
    t.text "city", null: false
    t.integer "postcode", null: false
    t.integer "maxSpace", null: false
    t.integer "availability", null: false
    t.index ["lotNumber"], name: "ParkingLot_lotNumber_uindex", unique: true
  end

  create_table "residence_staffs", primary_key: "staffNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "fName", null: false
    t.text "lName", null: false
    t.string "email", limit: 100, null: false
    t.text "homeCity", null: false
    t.string "street", limit: 50, null: false
    t.integer "postal", null: false
    t.date "dob", null: false
    t.text "gender"
    t.text "position", null: false
    t.string "location", limit: 50, null: false
    t.integer "phoneNumber", null: false
    t.integer "inspectedRoomID", null: false
    t.string "hall", limit: 50, null: false
    t.index ["inspectedRoomID"], name: "ResidenceStaff_StudentApartmentInspection_inspectedRoomID_fk"
    t.index ["staffNumber"], name: "ResidenceStaff_staffNumber_uindex", unique: true
  end

  create_table "room", primary_key: "placeNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string "rentRate", limit: 50, null: false
    t.text "availability", null: false
    t.integer "roomNumber"
    t.string "hallName", limit: 50
    t.integer "apartmentNumber"
    t.index ["apartmentNumber"], name: "room_StudentFlats_apartmentNumber_fk"
    t.index ["hallName"], name: "room_HallOfResidence_hallName_fk"
  end

  create_table "staff", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "fName"
  end

  create_table "student", primary_key: "mUNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "fName", null: false
    t.text "lName", null: false
    t.string "street", limit: 100, null: false
    t.text "city", null: false
    t.integer "postcode", null: false
    t.integer "phoneNumber", null: false
    t.string "email", limit: 100, null: false
    t.date "dob", null: false
    t.text "gender"
    t.text "standing", null: false
    t.text "nationality", null: false
    t.string "specialNeeds", limit: 100
    t.text "major", null: false
    t.string "statusOfStudent", limit: 100
    t.text "minor"
    t.text "comments"
    t.integer "advisorNumber", null: false
    t.index ["advisorNumber"], name: "Student_Advisor_advisorNumber_fk"
    t.index ["email"], name: "Student_email_uindex", unique: true
    t.index ["mUNumber"], name: "Student_mUNumber_uindex", unique: true
  end

  create_table "student_apartment_inspection", primary_key: "inspectedRoomID", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.text "inspectorName", null: false
    t.date "dateOfInspection", null: false
    t.text "apartmentCondition", null: false
    t.text "comments"
    t.index ["inspectedRoomID"], name: "StudentApartmentInspection_inspectedRoomID_uindex", unique: true
  end

  create_table "student_flats", primary_key: "apartmentNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.integer "numberOfRooms", null: false
    t.text "city", null: false
    t.string "street", limit: 50, null: false
    t.integer "postcode", null: false
    t.integer "leaseID", null: false
    t.index ["apartmentNumber"], name: "StudentFlats_apartmentNumber_uindex", unique: true
    t.index ["leaseID"], name: "StudentFlats_Lease_leaseNumber_fk"
  end

  create_table "vehicle", primary_key: "vNumber", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
    t.string "plateNumber", limit: 10, null: false
    t.text "color", null: false
    t.text "manufacturer", null: false
    t.string "brand", limit: 50, null: false
    t.integer "mUNum", null: false
    t.integer "lotNumber", null: false
    t.index ["lotNumber"], name: "Vehicle_ParkingLot_lotNumber_fk"
    t.index ["mUNum"], name: "Vehicle_Student_mUNumber_fk"
    t.index ["plateNumber"], name: "Vehicle_plateNumber_uindex", unique: true
    t.index ["vNumber"], name: "Vehicle_vNumber_uindex", unique: true
  end

  add_foreign_key "hall_of_residences", "residence_staffs", column: "staffNumber", primary_key: "staffNumber", name: "HallOfResidence_ResidenceStaff_staffNumber_fk"
  add_foreign_key "halls", "residence_staffs", column: "sfaffNumber", primary_key: "staffNumber", name: "halls_residence_staffs_staffNumber_fk"
  add_foreign_key "invoice", "lease", column: "leaseNumber", primary_key: "leaseNumber", name: "Invoice_Lease_leaseNumber_fk"
  add_foreign_key "invoice_payment", "student", column: "muNum", primary_key: "mUNumber", name: "InvoicePayment_Student_mUNumber_fk"
  add_foreign_key "lease", "student", column: "mUNum", primary_key: "mUNumber", name: "Lease_Student_mUNumber_fk"
  add_foreign_key "next_of_kin", "student", column: "mUNum", primary_key: "mUNumber", name: "NextOfKin_Student_mUNumber_fk"
  add_foreign_key "residence_staffs", "student_apartment_inspection", column: "inspectedRoomID", primary_key: "inspectedRoomID", name: "ResidenceStaff_StudentApartmentInspection_inspectedRoomID_fk"
  add_foreign_key "room", "hall_of_residences", column: "hallName", primary_key: "hallName", name: "room_HallOfResidence_hallName_fk"
  add_foreign_key "room", "student_flats", column: "apartmentNumber", primary_key: "apartmentNumber", name: "room_StudentFlats_apartmentNumber_fk"
  add_foreign_key "student", "advisor", column: "advisorNumber", primary_key: "advisorNumber", name: "Student_Advisor_advisorNumber_fk"
  add_foreign_key "student_flats", "lease", column: "leaseID", primary_key: "leaseNumber", name: "StudentFlats_Lease_leaseNumber_fk"
  add_foreign_key "vehicle", "parking_lot", column: "lotNumber", primary_key: "lotNumber", name: "Vehicle_ParkingLot_lotNumber_fk"
  add_foreign_key "vehicle", "student", column: "mUNum", primary_key: "mUNumber", name: "Vehicle_Student_mUNumber_fk"
end
zbdgwd5y

zbdgwd5y1#

您需要更改代码的这些部分

has_many :ResidenceStaffs

has_many :residence_staffs, primary_key: 'hallId', foreign_key: 'hall_id'

然后改变

belongs_to :Hall

belongs_to :hall, :primary_key: 'hallId', foreign_key: 'hall_id'

在架构中通过迁移进行更改

t.string "hall", limit: 50, null: false

t.integer "hall_id", null: false

您可以查看此文档以熟悉活动记录关联

相关问题