简体   繁体   English

具有许多地址,但该用户具有主要帐单,送货和资料地址的用户

[英]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表,并且在以下几个选项之间进行了弹跳:

  1. Make 3 columns on the 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列(这将消除对我认为的连接模型的需要)。
  2. Add a 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_idaddress_idpurpose (用途为billingshippingprofile )确定范围时,只有一个为真。
  3. Add a 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.

相关问题 一个用户多个送货地址如何设为默认地址 - One user multiple shipping address how to make it as default Magento:Paypal将发货地址作为默认地址,而不是帐单地址 - Magento: Paypal is taking shipping address as default instead of billing address 多对多原则:用户喜欢这篇文章吗? - Doctrine many to many: has user liked the article? 获取用户的主要电子邮件地址 mysql php 最佳实践 - Get user's primary email address mysql php best practices Doctrine - 如果用户实体可以有一个或多个地址,如何通过用户实体访问用户的活动地址? - Doctrine - How to access a User's active Address through the User entity when it can have one or more Addresses? 如何为一对多关系更新用户配置文件数据? - How to update user profile data for one to many relationship? 结构用户-有很多会话。 查找给定会话的用户 - Struct user - has many sessions. Find a user given a session 如何检查用户在个人资料中是否具有特定角色? - How do I check if a user has a certain role in their profile? php检查用户是否上传了个人资料图片 - php check whether the user has uploaded a profile pic or not 我应该有1或2个用户个人地址和用户组地址表吗? - Should I have 1 or 2 tables for user personal addresses and user group addresses?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM