0
在我app
我有兩個型號USER和PROFILE,我希望每個用戶只有一個輪廓,我試過has_one :profile
關聯,但它似乎並不工作,我想要的方式。如何阻止用戶創建多個配置文件?
我的用戶模型
class User < ActiveRecord::Base
has_one :profile
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.image = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
我的剖面模型
class Profile < ApplicationRecord
belongs_to :user
end
你想用哪種方式? –
如果用戶已擁有配置文件,或者您可以檢查是否存在用戶配置文件befor創建配置文件,則可以禁用配置文件創建表單。 – inye
您的用戶如何創建配置文件?如果它通過表單,我會禁用或不顯示錶單,如果用戶已經有一個配置文件。 –