さまざまなprofile
テーブルを作成して、プロファイルをユーザーに関連付けるだけです。したがって、ユーザーの種類ごとにテーブルを作成し、そこに特定の情報を保存して、参照するuser_id
列を作成できますusers
。
class User < ActiveRecord::Base
has_one :type_1
has_one :type_2
end
class Type1 < ActiveRecord::Base
belongs_to :user
end
class Type2 < ActiveRecord::Base
belongs_to :user
end
現在、これはあまり DRY ではなく、常にユーザー タイプを追加している場合に問題が発生する可能性があります。したがって、ポリモーフィズムを調べることができます。
ポリモーフィズムの場合、users
テーブルはユーザーのタイプ (profileable_id
およびprofileable_type
) を定義します。だから、このようなもの:
class User < ActiveRecord::Base
belongs_to :profileable, :polymorphic => true
end
class Type1 < ActiveRecord::Base
has_one :user, :as => :profileable
end
class Type2 < ActiveRecord::Base
has_one :user, :as => :profileable
end
次に、ユーザー タイプの STI (単一テーブル継承) の 3 番目のオプションがあります。しかし、ユーザー タイプ フィールドが大幅に異なる場合、それは適切に拡張できません。