[英]A User with many Addresses, but User has a primary billing, shipping, and profile Address
I'm building a Rails app where a User
can have many Addresses
. 我正在构建一个Rails应用程序,
User
可以在其中User
许多Addresses
。 A User
also needs a primary billing Address
, primary shipping Address
, and primary profile Address
. User
还需要一个主要账单Address
,一个主要运送Address
和一个主要配置文件Address
。 These primary fields can point to the same Address
. 这些主要字段可以指向相同的
Address
。 A User
cannot have more than one of any primary Address
. User
不能具有多个主Address
。
I've created a join table called AddressInfo
, and I'm bouncing between a few options: 我创建了一个名为
AddressInfo
表,并且在以下几个选项之间进行了弹跳:
User
model corresponding to each of the primary Address
ids (this would remove the need for the join model I think). User
模型上对应每个主要“ Address
ID的3列(这将消除对我认为的连接模型的需要)。 primary
boolean column to AddressInfo
, and make sure only one is true when scoped by user_id
, address_id
and purpose
(purpose being billing
, shipping
or profile
). primary
布尔列添加到AddressInfo
,并确保在按user_id
, address_id
和purpose
(用途为billing
, shipping
或profile
)确定范围时,只有一个为真。 primary
date time column to AddressInfo
, and use the most recently updated as the primary address (scoped like option 2). AddressInfo
添加一个primary
日期时间列,并使用最近更新的作为主要地址(类似于选项2)。 Maybe these options aren't the best, but it's what I've come up with so far. 也许这些选项不是最好的,但这是我到目前为止提出的。
Any help on how to resolve this issue would be appreciated. 任何有关如何解决此问题的帮助将不胜感激。
UPDATE: 更新:
To be clear, once an Address
is created it should always belong to that User
and be undeletable. 要明确的是,一旦创建了
Address
,它就应该始终属于该User
并且不可删除。 Ex. 防爆。 a
User
changes their primary billing address to a new Address
: they should still be able to retrieve that old Address
(maybe even make it a primary address again). User
将其主要帐单邮寄地址更改为新Address
:他们仍应能够检索该旧Address
(甚至可以再次将其用作主要邮寄地址)。 If I go with option 1 and remove the join table, that means I'll need a user_id
on Address
. 如果我选择选项1并删除联接表,则意味着我需要在
Address
上使用user_id
。
Go with option 1, 3 columns. 选择选项1,3列。 This will make less of a headache (as a programmer), will run faster, and is more flexible for doing things like combining similar addresses into one.
这将减少(作为程序员)的麻烦,运行速度更快,并且在执行诸如将类似地址组合为一个地址之类的操作时更加灵活。 Maybe you have 2 people with the same address, they could share the same record (not recomeneded though).
也许您有2个人使用相同的地址,他们可以共享相同的记录(不过不推荐)。
If you're using rails I would look to a rails solution to 'stay on the rails'. 如果您使用的是护栏,那么我会考虑使用护栏解决方案以“保持在护栏上”。
I would consider STI ( Single Table Inheritance ) 我会考虑STI( 单表继承 )
More info at Rails: Need help defining association for address table Rails上的更多信息:需要帮助来定义地址表的关联
or a Polymorphic relationship - http://guides.rubyonrails.org/association_basics.html#polymorphic-associations 或多态关系-http: //guides.rubyonrails.org/association_basics.html#polymorphic-associations
As you want 1 user to have multiple address (rather than ther being multiple user types) than STI may be best for you. 因为您希望1个用户拥有多个地址(而不是多个用户类型),所以STI可能最适合您。
Note: they can also be combined, eg http://www.archonsystems.com/devblog/2011/12/20/rails-single-table-inheritance-with-polymorphic-association/ 注意:它们也可以组合使用,例如http://www.archonsystems.com/devblog/2011/12/20/rails-single-table-inheritance-with-polymorphic-association/
There's a great example of STI addressing this issue at: http://blog.arkency.com/2013/07/sti/ http://blog.arkency.com/2013/07/sti/提供了一个STI解决此问题的好例子。
Another possibility is to create another model to hold user.id, addressinfo.id, and an primary_for_address_type (containing "Shipping", "Profile", "Billing"). 另一种可能性是创建另一个模型来保存user.id,addressinfo.id和primary_for_address_type(包含“发货”,“配置文件”,“账单”)。
Constrain this to be unique on user and primary_for_address_type, and you can tag addresses in AddressInfo as being the primary for particular address types in a completely extensible way that still guarantees uniqueness of the primary address. 将其限制为在用户和primary_for_address_type上是唯一的,并且可以以完全可扩展的方式在AddressInfo中将地址标记为特定地址类型的主要地址,从而仍然保证主要地址的唯一性。
You might join directly from this model to address, but there's scope for getting it out of sync with your addressinfo model. 您可能直接从此模型加入到地址,但是有一些使它与addressinfo模型不同步的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.