如何关联Rails中的列? (PostgreSQL系统)

时间:2017-12-15 18:16:50

标签: ruby-on-rails database postgresql shopify

我有2个型号:

class Address < ApplicationRecord
  belongs_to :shop
end

class Shop < ActiveRecord::Base
 include ShopifyApp::SessionStorage
 has_many :addresses
end

with:

class CreateAddresses < ActiveRecord::Migration[5.1]
  def change
  create_table    :addresses do |t|
   t.string      :address_1
   t.string      :address_2
   t.string      :postal
   t.string      :phone
   t.string      :city
   t.timestamps
  end
end
end

class CreateShops < ActiveRecord::Migration[5.1]
 def self.up
   create_table :shops  do |t|
      t.integer :shopify_id, :limit => 8, null: false
      t.string :shopify_domain, null: false
      t.string :shopify_token, null: false
      t.jsonb :shop_json, null: false
      t.timestamps
   end

  add_index :shops, :shopify_domain, unique: true
 end

 def self.down
   drop_table :shops
 end
end

我想参考&#39; shopify_id&#39; Shop表中的列,其中包含一个名为&#39; shopify_id&#39; (在地址表中包含相同的值)。

我怎么能这样做?

0 个答案:

没有答案