2017-02-24 60 views
0

我有一個應用程序,客戶可以下訂單以將項目發送到目的地。訂單需要跟蹤客戶和目的地地址。添加訂單會使活動記錄關聯複雜化

我開始用下面的活動記錄關聯:

CUSTOMER 
has_one :customer_address 

CUSTOMER_ADDRESS 
belongs_to :customer 

DESTINATION 
has_one :destination_address 

DESTINATION_ADDRESS 
belongs_to :destination 

現在我想添加一個命令的概念。

所以我提出以下修改:

CUSTOMER 
has_one :customer_address 
has_many :orders 

CUSTOMER_ADDRESS 
belongs_to :customer 

DESTINATION 
has_one :destination_address 

DESTINATION_ADDRESS 
belongs_to :destination 

ORDER 
belongs_to :customer 
has_one :customer_address, through: :customer 
has_one :destination_address, through :destination 

兩個問題:

  1. 有在ORDER兩個has_one協會沒有對稱belongs_to。這似乎是錯誤的,但它對於客戶或目的地在概念上也是沒有意義的,部分是因爲訂單和客戶或目的地只有一個地址。

  2. ORDER的正確遷移是什麼?

在此先感謝。

回答

0

遷移會是這個樣子:

class CreateOrder < ActiveRecord::Migration 
    def change 
    create_table :orders do |t| 
     t.string :name 
     # add price, products etc. 
     t.integer :destination_id 
     t.timestamps 
    end 
    end 
end 

你還需要一個連接表許多customers鏈接到許多orders。像這樣:

class CreateCustomerOrders < ActiveRecord::Migration 
    def change 
    create_table :customer_orders do |t| 
     t.integer :customer_id 
     t.integer :order_id 
     t.timestamps 
    end 
    end 
end