三通认证关系。如何设置?

时间:2021-04-08 12:33:21

I've implemented facebook and twitter authentication in an app I'm working on by following the excellent Railscasts videos on the topic.

我在一个正在开发的应用程序中实现了facebook和twitter认证,方法是跟踪关于这个主题的优秀的铁路广播视频。

Thus far I have my user model hooked up with Devise and I have my Authentications model built and recording authentication providers when users sign up via them. It looks like this:

到目前为止,我已经将我的用户模型与devices连接在一起,我已经构建了身份验证模型,并在用户通过它们注册时记录身份验证提供者。它看起来像这样:

id | user_id | provider | uid
-----------------------------
1  |    1    | facebook | xyf
2  |    1    | twitter  | pfr
3  |    2    | facebook | sdf

Now, the next thing I need is to store some of the data that these omniauth providers give me. Facebook for example provides the users gender and the url for their facebook profile. A connection to twitter on the other hand will provide different information on a user.

现在,我需要做的下一件事是存储这些omniauth提供者提供给我的一些数据。例如,Facebook为用户提供性别和他们的Facebook配置文件的url。另一方面,与twitter的连接将为用户提供不同的信息。

This leads me to believe that I should have two tables, one for twitter data (TwitterProfile) and one for facebook data (FacebookProfile).

这让我相信我应该有两个表,一个用于twitter数据(TwitterProfile),一个用于facebook数据(FacebookProfile)。

So should I extend my authentications model like this?

那么我应该像这样扩展我的身份验证模型吗?

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            | 

and the my models/user.rb would look something like:

和我的模型/用户。rb看起来是这样的:

has_many :authentications, :dependent => :destroy
has_one :facebook_profile, :through => :authentications, :source => :facebook_profile
has_one :twitter_profile, :through => :authentications, :source => :twitter_profile

But can my authentications model belong to 3 tables?

但是我的身份验证模型是否属于3个表?

  belongs_to :user
  belongs_to :facebook_profile, :class_name => "FacebookProfile"
  belongs_to :twitter_profile, :class_name => "TwitterProfile"

It just seems like I'm on the wrong track because I have redundant space in my authentications model ant the relationships seem overly complicated.

似乎我走错了方向因为我的身份验证模型中有冗余空间关系看起来过于复杂。

1 个解决方案

#1


1  

You might want to consider a polymorphic association. Say the association is called Profile. Basically, the association has a single profile_id, and a string profile_type column, which indicates what type of profile each authentication has.

您可能需要考虑多态关联。说这种联系叫做轮廓。基本上,关联有一个profile_id和一个string profile_type列,该列指示每个身份验证具有什么类型的概要文件。

There are lots of resources explaining how to set this up including a Railscast video, a section in the Rails Guides and it's in the Rails API too.

有很多资源解释如何设置它,包括Railscast视频,Rails指南中的一节,它也在Rails API中。

#1


1  

You might want to consider a polymorphic association. Say the association is called Profile. Basically, the association has a single profile_id, and a string profile_type column, which indicates what type of profile each authentication has.

您可能需要考虑多态关联。说这种联系叫做轮廓。基本上,关联有一个profile_id和一个string profile_type列,该列指示每个身份验证具有什么类型的概要文件。

There are lots of resources explaining how to set this up including a Railscast video, a section in the Rails Guides and it's in the Rails API too.

有很多资源解释如何设置它,包括Railscast视频,Rails指南中的一节,它也在Rails API中。