我是 RoR 的新手,我很难创建属于用户的授权实例。当用户不存在时它工作正常,创建用户并且它是第一次授权。但是当我尝试为现有用户创建授权时,我得到了 AssociationTypeMismatch。任何想法 ?
这是我的用户类。
class User < ActiveRecord::Base
has_many :authorizations, :dependent => :destroy
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end
我的授权类
class Authorization < ActiveRecord::Base
attr_accessible :accesskey, :code, :provider_id, :refreshkey, :user_id, :authorization, :user, :user_attributes
belongs_to :user
end
以及在 Authorization.create 上返回错误的方法
begin generatedauthorization
...
user_email = s['email']
#check if user already exists
user = User.where(:email => user_email)
if user.any?
flash[:notice] = 'Old user'
else
password = 'uyflgmugmiuymmb'
user = User.create(:email => user_email, :password => password, :password_confirmation => password)
end
Authorization.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token, :user => user)
end