【发布时间】:2016-04-20 18:11:00
【问题描述】:
我有两个模型,一个是用户,另一个是个人资料
class User < ActiveRecord::Base
has_one :profile
delegate :first_name, :last_name, :email, :phone, :location, to: :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
如何验证用户个人资料的唯一性,因为每次对于同一个用户,它在用户注销并再次通过omniauth 登录后创建许多个人资料
如下图
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid).permit!).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.refresh_token = auth.credentials.refresh_token
user.build_profile(first_name: auth.info.first_name, last_name: auth.info.last_name, email: auth.info.email, location: auth.info.location, phone: auth.info.phone)
user.save!
end
end
如何为 uniq 用户制作唯一的优先文件。
一种方法
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid).permit!).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.refresh_token = auth.credentials.refresh_token
user.auth_token = auth.credentials.token
user.instance_url = auth.credentials.instance_url
user.save!
if Profile.exists?(email: auth.info.email)
user.profile = Profile.where(email: auth.info.email).first
user.save
else
user.build_profile(first_name: auth.info.first_name, last_name: auth.info.last_name,\
email: auth.info.email, location: auth.info.location, phone: auth.info.phone).save
end
end
end
但这不是一个好办法
我知道这是非常基本的,但想要一个最好的方法..
提前致谢
【问题讨论】:
-
has_one 应该会产生一个唯一的条目,通过覆盖以前的条目,您是否看到了其他内容?
-
感谢 Brad 的回复,是的,每次登录后都会创建重复的配置文件我使用的是 rails 4.2.3
-
我的意思是 User.first.profile 将显示一个配置文件,但如果我这样做 Profile.all 我有很多重复记录意味着每次登录它都会创建配置文件我如何检查防止此重复记录的最佳方法创作
标签: ruby-on-rails ruby ruby-on-rails-4