User
모델에 has_one Profile
(belongs_to User)이 있습니다. 간단히 말해 프로파일에 location
필드와 occupation
필드가 있다고 가정 해 보겠습니다.레일 : 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 with build_profile
메서드에 대한 액세스 권한을 알고 있지만 내 User 개체에 제대로 연결되어 있는지 확인하려고합니다. 객체와 함께 Profile
을 올바르게 빌드하려면 해당 작업을 다시 포맷해야하는 방법에 대한 통찰력이 매우 유용합니다.
최소한 두 가지 방법이 있으며 세 번째 줄을'user = User.create'로 변경하면'user.create_profile()'을 할 수 있습니다. 'User.create'와'user.create_profile' 대신에 build를 사용하고'user.save'로 한번에 저장하십시오. –