如何在Ruby on Rails中加入MySQL表?

时间:2021-05-31 11:25:20

I am very new to rails and working with databases in general. I am trying simply display a list of all of the residence_staff members who are 'managers' and the hallName of the hallOfResidence that they work in. residence_staff is 1 table with the field 'fName' and the name of the hall as a foreign key to the hallOfResidence table. How can I join the two tables and display it to the screen?

我非常擅长使用rails并且通常使用数据库。我正在尝试简单地显示所有作为'经理'的residence_staff成员和他们工作的hallOfResidence的hallName的列表.stost_staff是一个带有'fName'字段的表,以及作为外键的hall的名称hallOfResidence表。如何加入这两个表格并将其显示在屏幕上?

Currently I am only displaying a list of all of the residence_staff members.

目前我只显示所有residence_staff成员的列表。

Staff model:

员工模特:

class ResidenceStaff < ApplicationRecord
  validates :fName, presence: true

end

Halls model:

大厅型号:

class HallOfResidence < ApplicationRecord
end

Controller:

控制器:

class HomeController < ApplicationController

  def index
    @residence_staff = ResidenceStaff.all
  end
end

View

视图

<h1>Title</h1>

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

1 个解决方案

#1


1  

Caveat Emptor: Code Not Tested.

警告Emptor:代码未经测试。

Given something like (you really should use hall_of_residence_id as the foreign key):

给出类似的东西(你真的应该使用hall_of_residence_id作为外键):

# == Schema Information
#
# Table name: residence_staffs
#
#  id                                :integer          not null, primary key
#  first_name                        :string
#  hall_of_residence_id              :integer
#  position                          :string
#  created_at                        :datetime         not null
#  updated_at                        :datetime         not null
#
class ResidenceStaff < ApplicationRecord
  validates :first_name, presence: true
  belongs_to :hall_of_residence

  class << self

    def managers
      where(position: 'manager')
    end

  end
end

And:

和:

# == Schema Information
#
# Table name: hall_of_residences
#
#  id                                :integer          not null, primary key
#  name                              :string
#  created_at                        :datetime         not null
#  updated_at                        :datetime         not null
#
class HallOfResidence < ApplicationRecord
  has_many :residence_staffs
end

It seems like you should be able to do something like (see the Guide for information on includes):

看起来您应该能够做类似的事情(有关包含的信息,请参阅指南):

class HomeController < ApplicationController

  def index
    @residence_staffs = ResidenceStaff.all
    @residence_staff_managers = ResidenceStaff.managers.includes(:hall_of_residence)
  end

end

And:

和:

<h1>Title</h1>

<div>
  <% @residence_staff_managers.each do |residence_staff| %>
    <%= residence_staff.first_name>
    <%= residence_staff.hall_of_residence.name >
  <% end %>
</div>

#1


1  

Caveat Emptor: Code Not Tested.

警告Emptor:代码未经测试。

Given something like (you really should use hall_of_residence_id as the foreign key):

给出类似的东西(你真的应该使用hall_of_residence_id作为外键):

# == Schema Information
#
# Table name: residence_staffs
#
#  id                                :integer          not null, primary key
#  first_name                        :string
#  hall_of_residence_id              :integer
#  position                          :string
#  created_at                        :datetime         not null
#  updated_at                        :datetime         not null
#
class ResidenceStaff < ApplicationRecord
  validates :first_name, presence: true
  belongs_to :hall_of_residence

  class << self

    def managers
      where(position: 'manager')
    end

  end
end

And:

和:

# == Schema Information
#
# Table name: hall_of_residences
#
#  id                                :integer          not null, primary key
#  name                              :string
#  created_at                        :datetime         not null
#  updated_at                        :datetime         not null
#
class HallOfResidence < ApplicationRecord
  has_many :residence_staffs
end

It seems like you should be able to do something like (see the Guide for information on includes):

看起来您应该能够做类似的事情(有关包含的信息,请参阅指南):

class HomeController < ApplicationController

  def index
    @residence_staffs = ResidenceStaff.all
    @residence_staff_managers = ResidenceStaff.managers.includes(:hall_of_residence)
  end

end

And:

和:

<h1>Title</h1>

<div>
  <% @residence_staff_managers.each do |residence_staff| %>
    <%= residence_staff.first_name>
    <%= residence_staff.hall_of_residence.name >
  <% end %>
</div>