Ruby on Rails模型无法识别预加载?

时间:2018-04-26 06:22:20

标签: mysql ruby-on-rails database model-view-controller mariadb

我正在尝试在mariadb数据库中连接两个表。大厅桌和residence_staff表。目标是显示每个大厅的经理的所有名称以及他们工作的大厅。当我运行应用程序时,我收到此错误:Hall(hallId:整数,hallName:文本,街道:文本,城市:文本,邮政编码:整数,phoneNumber:整数,sfaffNumber:整数)无法识别预加载

我不知道这意味着什么。

Halls模型

class Hall < ApplicationRecord
  has_many :ResidenceStaffs
end

residence_staff模型

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的一个问题,但我不完全确定。此外,

Schema.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

1 个答案:

答案 0 :(得分:0)

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

<html>

<head>
    <title>
        Validation
    </title>
    <script type="text/javascript" language="javascript">
        function myfunction()
        {
        var a=GetElementById("log");
        var b=GetElementById("pwd");
        if (a=="log" && b=="pwd")
        {
            window.open("https://www.google.com");
        }
        else
        {
            alert("Your username or password is wrong");
        }
        }
    </script>
</head>
<body>
    <form method="post">
        Name: <input type="textbox" id="log" name="log"> <br>
        Password: <input type="password" id="pwd" name="pwd"> <br>
        <input type="submit" value="submit" onsubmit="myfunction();">
    </form>
</body>

has_many :ResidenceStaffs

然后改变

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

belongs_to :Hall

通过ADMIN_staffs架构中的迁移进行更改

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

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

您可以查看this doc以熟悉活动记录关联