Reputation: 15692
Let's say I have these:
class User
include Mongoid::Document
field :name, type: String
field :email, type: String
embeds_many :auths
attr_protected :name, :email
end
class Auth
include Mongoid::Document
field :provider
field :uid
embedded_in :user
attr_protected :provider, :uid
end
I create a new user using this method:
def self.create_with_omniauth(auth)
create! do |user|
user.auths.build(provider: auth['provider'], uid: auth['uid'])
if auth['info']
user.name = auth['info']['name'] || ''
user.email = auth['info']['email'] || ''
end
end
end
However, when I look into my database, the result is this:
{
"_id" : ObjectId("517f5f425aca0fbf3a000007"),
"name" : "User",
"email" : "[email protected]",
"auths" : [
{
"_id" : ObjectId("517f5f425aca0fbf3a000008")
}
]
}
What do I have to do in order to actually save the data provided? The uid
and the provider
are always properly in the auth
array, I checked.
Upvotes: 1
Views: 1259
Reputation: 115521
Currently attributes are just skipped since that's what you tell Rails
Either change:
attr_protected :provider, :uid
to:
attr_accessible :provider, :uid
or proceed as follows:
user.auths.build.tap do |user_auth|
user_auth.provider = auth['provider']
user_auth.uid = auth['uid']
end
Upvotes: 2
Reputation: 5902
Can you try this?
def self.create_with_omniauth(auth)
create! do |user|
auth = Auth.build(provider: auth['provider'], uid: auth['uid'])
if auth['info']
user.name = auth['info']['name'] || ''
user.email = auth['info']['email'] || ''
end
user.auths << auth
end
end
Upvotes: 1