omni-auth認証に使用したUser
モデルがあります。 Facebook、Googleアカウントなどのさまざまなプロバイダがサインインに使用できるように設定しました。これらのプロバイダを使用してサインアップしているときに電子メールがデータベースに既に存在する場合、そのアカウントにユーザがサインインされます。mongoidにembeds_manyアソシエーションを持つfirst_or_createメソッド
class User include Mongoid::Document embeds_many :providers field :email,type: String, default: "" end
class Provider include Mongoid::Document field :name, type: String field :uid, type: String embedded_in :user end
そして私は、認証情報に基づいてユーザーを見つけることfrom_omniauth
メソッドを使用しています。
def from_omniauth(auth) user=User.find_by(email: auth.info.email) return user if user where(:"providers.name" => auth.provider, :"provider.uid" => auth.uid).first_or_create do |user| user.email = auth.info.email user.password = Devise.friendly_token[0,20] end end
しかし、それは1を作成するには、ユーザーと試行を見つける傾けるとき、私はエラーを取得しています。 Mongoid::Errors::UnknownAttribute: message: Attempted to set a value for 'providers.name' which is not allowed on the model User.
しかし、これは私がすでに関連付けを定義しているので、なぜこれを動的フィールド生成と見なしますか?私もfind_or_initialize
メソッドを試したが、同じエラー。
誰かが私にそれを理解させるのに役立つことができますか?前もって感謝します。