Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby on Rails w/Devise-预期的AssociationTypeMitch用户_Ruby On Rails_Ruby_Associations - Fatal编程技术网

Ruby on rails Ruby on Rails w/Devise-预期的AssociationTypeMitch用户

Ruby on rails Ruby on Rails w/Devise-预期的AssociationTypeMitch用户,ruby-on-rails,ruby,associations,Ruby On Rails,Ruby,Associations,我是RoR的新手,我正在努力创建一个属于用户的授权实例。当用户不存在时,它可以正常工作,创建用户及其第一次授权。但是当我尝试为现有用户创建授权时,我得到了AssociationTypeMismatch。 有什么想法吗 这是我的用户类 class User < ActiveRecord::Base has_many :authorizations, :dependent => :destroy devise :database_authenticatable, :registe

我是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
而不是

Authorization.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token, :user => user)

user.authorizations.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token)

user=user.where(:email=>user\u email)
替换为
user=user.find\u by\u email(user\u email)

然后
user.authorizations.create(:provider\u id=>1,:code=>code,:refreshkey=>refresh\u token,:accesskey=>access\u token)

#其中
返回
关系
对象,该对象允许其他ActiveRecord方法,如
顺序
链接。要按电子邮件搜索属性,建议使用搜索帮助程序
find\u by\u email
,它返回
User
对象。

要使其与
where
user=user.where(:email=>user\u email)。首先

感谢Pandurang,但我得到了一个命名错误:未定义的方法“authorizations”。我假设这是因为我的方法在我的授权控制器中。不,不管你在哪个控制器中,你能指定完全错误吗?是不是说“未定义的方法‘授权’用于nil类”?未定义的方法‘授权’用于#ohk,然后执行此user=user.where(:email=>user\u email)。首先代替user=user.where(:email=>user\u email)Pandurang,做得好。但是,在这种情况下,避免使用
where
;改用
通过电子邮件查找
。其他一切都是正确的。另外,将
if-user.any?
替换为
if-user
即可。感谢专业提示^_^
Authorization.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token, :user => user)
user.authorizations.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token)