Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

postgresql - Joining two tables in 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 %>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...