トピックに関する優れたRailscastsビデオに従って、取り組んでいるアプリにfacebookとtwitter認証を実装しました。
これまでのところ、ユーザー モデルをDeviseに接続し、認証モデルを構築して、ユーザーがサインアップしたときに認証プロバイダーを記録しました。次のようになります。
id | user_id | provider | uid
-----------------------------
1 | 1 | facebook | xyf
2 | 1 | twitter | pfr
3 | 2 | facebook | sdf
さて、次に必要なことは、これらの omniauth プロバイダーが提供するデータの一部を保存することです。たとえば、Facebook は、ユーザーの性別と Facebook プロファイルの URL を提供します。一方、Twitter への接続は、ユーザーに関するさまざまな情報を提供します。
これにより、1 つは Twitter データ (TwitterProfile) 用、もう 1 つは facebook データ (FacebookProfile) 用の 2 つのテーブルが必要であると思われます。
では、このように認証モデルを拡張する必要がありますか?
id | user_id | provider | uid | facebook_profile_id | twitter_profile_id
------------------------------------------------------------------------
1 | 1 | facebook | xyf | 1 |
2 | 1 | twitter | pfr | | 2
3 | 2 | facebook | sdf | 2 |
私の models/user.rb は次のようになります。
has_many :authentications, :dependent => :destroy
has_one :facebook_profile, :through => :authentications, :source => :facebook_profile
has_one :twitter_profile, :through => :authentications, :source => :twitter_profile
しかし、私の認証モデルは 3 つのテーブルに属することができますか?
belongs_to :user
belongs_to :facebook_profile, :class_name => "FacebookProfile"
belongs_to :twitter_profile, :class_name => "TwitterProfile"
認証モデルに余分なスペースがあり、関係が複雑すぎるように見えるため、間違った方向に進んでいるようです。