我有一个User
模型has_one Profile
(belongs_to用户)。为简单起见,我只是说配置文件有一个location
字段和一个occupation
字段。Rails:使用Omniauth在模型中建立一个has_one关系
我使用Omniauth
来创建用户,但我也想同时创建附加的Profile
。目前我create
从omniauth方法如下:
def self.create_from_omniauth(omniauth_data)
full_name = omniauth_data["info"]["name"].split(" ")
User.create(
provider: omniauth_data["provider"],
uid: omniauth_data["uid"],
first_name: full_name[0],
last_name: full_name[1],
email: omniauth_data["info"]["email"],
password: SecureRandom.hex(16)
)
end
我不知道我应该怎么包括该配置文件的建设。我知道has_one我有权访问build_profile
方法,但我也想确保它已正确连接到我的用户对象。任何有关如何重新格式化create_from_omniauth
操作以正确构建与User
对象相关联的Profile
都非常有帮助。
至少有两种方法可以做到这一点,如果您将第三行更改为'user = User.create',则可以执行'user.create_profile()'。而不是'User.create'和'user.create_profile'使用build,然后用'user.save'一次性保存它。 –