Rails的:需要帮助确定协会地址表 [英] Rails: Need help defining association for address table
问题描述
我发现这是一个有点头脑弯曲,并希望一些想法。我的设计去喜欢这样的:
I finding this a bit mind bending and would like some ideas. My design goes likes this:
- 在这里的建设者表,每个制造商可以有一个邮寄地址
- 有一个客户表中,每个客户可以有一个邮寄地址和一个帐单地址
- 有一个地址表
我需要定义的建设者,地址和客户端,地址之间的关联
I need to define the associations between the builders-addresses and clients-addresses
我最初的数据库设计是这样的:
My initial database design was like this:
Builders
--------------------
id
postal_address_id
...
Clients
--------------------
id
postal_address_id
billing_address_id
...
Addresses
-------------------
id
...
这似乎是合乎逻辑给我,但我有麻烦,在轨协会翻译这一点。
This seems logical to me, but I am having trouble translating this in rails associations.
我在想,地址可以定义一个belongs_to的:寻址,多态关联。但如何我为客户办理邮政和账单地址。
I am thinking that addresses could define a belongs_to :addressable, polymorphic association. But how to I handle postal and billing addresses for the clients.
任何帮助将大大AP preciated。
Any help will be much appreciated.
推荐答案
在这样的我会用STI(单个表继承)的情况下,我认为这是很好的例子,这一点。
In a case like that I would use STI (Single table inheritance), I think it's the perfect case for that.
首先,你必须在你的地址模式可以添加一个字段类型。
然后,你可以定义你喜欢的类:
First, you have to add a field "type" in your address model.
Then you can define you classes like that :
class Client
has_one :billing_address, :as => :addressable
has_one :shipping_address, :as => :addressable
end
class Address < ActiveRecord:Base
belongs_to :addressable, :polymorphic => true
end
class BillingAddress < Address
end
class Shipping Address < Address
end
了解更多关于性病在轨DOC
这篇关于Rails的:需要帮助确定协会地址表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!