我正在rails上运行rubocop,它给了我下面的消息。
W: Shadowing outer local variable - user.
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
^^^^
这就是代码。
def self.from_omniauth(auth)
user = User.where(email: auth.info.email).first
if user
return user
else
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.fullname = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.image = auth.info.image
user.password = Devise.friendly_token[0, 20]
end
end
结束
这意味着作为块参数提供的user
将覆盖此处定义的user
变量user = User.where(email: auth.info.email).first
为了克服这个问题,您需要更改其中一个变量的名称。或者类似于:
result = User.where...
return result if result
或者:
where(provider: auth.provider, uid: auth.uid).first_or_create do |u|
u.fullname = auth.info.name
...
end
更多信息:https://github.com/bbatsov/ruby-style-guide#no-阴影