如何在rubyonrails中连接表?

xkrw2x1b  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(221)

我有一张table residence_staff 以及 halls . 在 residence_staff 表中有一个字段 position 如果是manager,我想显示一个所有manager和 hallName ,他们工作的地方。我可以打印经理的名字,但应用程序无法识别 hallName . 这些表是使用 staffNumber 作为 halls table,到 staffNumberresidence_staff table。我做错什么了? residence_staff 型号:

class ResidenceStaff < ApplicationRecord
  belongs_to :hall, primary_key: 'hallId'

  class << self

    def managers
      where(position: 'manager')
    end
  end
end
``` `halls` 型号:

class Hall < ApplicationRecord
has_many :residence_staffs
end

控制器:

class HomeController < ApplicationController

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

查看:

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", 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

cl25kdpy

cl25kdpy1#

而不是 <%= residence_staff.hallName %> 使用 <%= residence_staff.hall.hallName %> 因为 hallName 是的属性 hall .
另一方面,ruby命名约定更倾向于snake case。 hallName 会是 hall_name 相反。

相关问题