2017-02-24 6 views
0

私は、顧客が注文をして商品を宛先に送ることができるアプリを持っている。注文は、顧客と宛先アドレスの両方を追跡する必要があります。注文を追加するとアクティブなレコードの関連付けが複雑になる

私は、次のActive Recordの団体で始まる:

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. ために、2つのhas_one団体のための対称belongs_toありません

    は、だから私は、次のように変更します。これは間違っているように見えますが、顧客または宛先のために多くの注文と唯一の住所があるため、顧客または宛先の概念が概念的にはbelong_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 

ます。また、多くのordersに多くのcustomersをリンクする結合テーブルが必要になります。このようなもの:

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 
関連する問題