设计Rails应用程序:单表inheritance?

我已经研究了几天了,我知道有很多文章讨论单表inheritance和与Rails的多态关联。 很多有用的资料来自2009年或之前,我想知道现在是否有更好的方法解决这个问题。

情况

该应用程序有几种不同的用户类型(即买方和卖方),每种类型都有一个配置文件。 每种用户类型的配置文件确实不同,所以我目前排除了一个通用“配置文件”表的想法。

我目前的做法

这与此解决方案相同。

class User < ActiveRecord::Base # authentication stuff end class UserType1 < User has_one :user_type_1_profile end class UserType2 < User has_one :user_type_2_profile end ... class UserTypeN < User has_one :user_type_n_profile end 

根据我的研究,这是一种“混合模型”设计。

老实说,在这一点上,我不知道任何其他可行的解决方案。 每当我看到类似的问题时,我都会看到多态关联的想法。 有人可以详细说明多态关联在这种情况下如何工作?

有没有人有任何其他设计建议?

在这里使用各种配置文件类型的多态关联会更好。

 class User < ActiveRecord::Base  belongs_to :profile, :polymorphic => true end class ProfileTypeA < ActiveRecord::Base  has_one :user, :as => :profile end class ProfileTypeB < ActiveRecord::Base  has_one :user, :as => :profile end 

这需要你有一个像这样的迁移/表:

 change_table :users do |t| t.integer :profile_id t.string :profile_type # OR (same thing, the above is more explicit) t.references :profile, :polymorphic => true end 

“指南”中有更多相关信息: http : //guides.rubyonrails.org/association_basics.html#polymorphic-associations