在Rails中联接两个表 [英] Joining two tables in rails

查看:78
本文介绍了在Rails中联接两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试链接位置和操作表,以便可以在操作视图的位置表中显示一些数据。但是,我被困住了,不知道该怎么办。任何帮助,将不胜感激。

I am trying to link the location and the operating tables so that I can display some data in location table in the operatings views. However, I am stuck and don't know what to do. Any help would be appreciated.

 #below are models#
 class Location < ApplicationRecord  
    has_many :operatings
 end

 class Operating < ApplicationRecord
    belongs_to :location
 end

 ##below are my tables##

 enable_extension "plpgsql"

 create_table "locations", force: :cascade do |t|
    t.string   "country"
    t.string   "supra_region"
    t.string   "region"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
 end

 create_table "operatings", force: :cascade do |t|
  t.string   "operating_company_name"
  t.string   "address"
  t.date     "year_formed"
  t.string   "other_operational_countries"
  t.string   "about_company"
  t.string   "current_focus"
  t.string   "incumbent_irm_contractor"
  t.string   "irm_frame_agreements"
  t.text     "estimated_irm_budgets"
  t.integer  "location_id"
  t.datetime "created_at",                  null: false
  t.datetime "updated_at",                  null: false
  t.index ["location_id"], name: "index_operatings_on_location_id", using: :btree
 end

 add_foreign_key "operatings", "locations"

 ###below is my operating controller###

  def create
     @operating = Operating.new(op_company)
     if @operating.save
        flash[:success] = "A recorded has been successfully Saved"
        redirect_to operatings_path
     else
        render 'new'
     end
  end

 ####routes####
 resources :offshores,    :index, :show, :new, :create, :destroy
 resources :locations,    :index, :show, :new, :create, :destroy


推荐答案

因为您的位置 Operating 模型使用 has_many belongs_to ,如果模板中有 operating 对象,则可以轻松访问其位置的属性:

Since your Location and Operating models are linked together using has_many and belongs_to, if you have an operating object in your template, you can easily access the attributes of its location:

<% @operatings.each do |operating| %>
  <div>The name of its location: <%= operating.location.name %></div>
<% end %>

不过,您需要对此小心。如果仅从数据库中获取操作,则在每个每个循环中访问每个操作的 location 属性将触发一个单独的数据库查询每个操作项目。这称为 N + 1 查询,效率非常低。要解决此问题,在使用 includes 加载操作时,请确保预取相关的位置:

You need to be careful with this though. If you only fetch the operatings from the database, accessing each operating's location attribute in that each loop will trigger a separate database query for every operating item. This is called an N+1 query, and it is very inefficient. To fix the problem, make sure to pre-fetch the associated location as well when loading operatings using includes:

# in the controller
@operatings = Operating.all.includes(:location)

这样,每个操作的关联位置将仅通过单个查询获取。

This way the associated locations of every operating will be fetched using just a single query.

这篇关于在Rails中联接两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆