【发布时间】:2017-02-24 03:46:30
【问题描述】:
我有一个应用程序,客户可以在其中下订单以将物品发送到目的地。订单需要同时跟踪客户地址和目的地地址。
我从以下 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
两个问题:
对于 ORDER 中的两个
has_one关联,没有对称的belongs_to。这似乎是错误的,但从概念上来说,belong_toORDER 的客户或目的地也没有任何意义,部分原因是有很多订单,而客户或目的地只有一个地址。什么是 ORDER 的正确迁移?
提前致谢。
【问题讨论】:
标签: ruby-on-rails activerecord