Rails中的Omniauth / MYSQL尴尬

时间:2013-07-09 15:39:12

标签: ruby-on-rails facebook omniauth

我正在使用'omniauth'和'omniauth-facebook'宝石用于Rails 3.2,使用Ruby 2.0,今天我遇到了一个尴尬的情况,我找不到任何解决方案。每个用户都在使用facebook登录,直到此为止。

我的路线

# Facebook authentication
match 'auth/:provider/callback' => 'sessions#create'

我的会话控制器

class SessionsController < ApplicationController


 def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.uid
    redirect_to user
  end

....

我的会话模型

def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
    user.provider = auth.provider
    user.id = auth.uid
    user.uid = auth.uid
    user.name = auth.info.name
    if !user.username
        user.username = auth.uid
    end
    user.username = auth.info.nickname
    user.email = auth.info.email
    user.gender = auth.extra.raw_info.gender
    user.is_admin = false
    user.picture = "http://graph.facebook.com/#{auth.uid}/picture?type=large&height=324&width=580"
    user.updated_at = auth.extra.raw_info.updated_time
    user.oauth_token = auth.credentials.token
    user.oauth_expires_at = Time.at(auth.credentials.expires_at)
    user.save!
end
end

让我告诉你尴尬的部分。用户通过身份验证,我们称之为TESTUSER,但Rails重定向并设置会话ID与其原始ID不同,而不是当前的ID。

 User Load (132.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`provider` = 'facebook' AND `users`.`uid` = 100006300277396 LIMIT 1
   (129.6ms)  BEGIN
  SQL (130.6ms)  INSERT INTO `users` (`age`, `bio`, `created_at`, `email`, `experience`, `fat_percentage`, `gender`, `height`, `id`, `is_admin`, `location`, `mass`, `name`, `oauth_expires_at`, `oauth_token`, `password`, `picture`, `provider`, `salt`, `uid`, `updated_at`, `username`) VALUES (NULL, NULL, '2013-07-09 15:32:14', 'TESTUSER@yandex.com', NULL, NULL, 'male', NULL, 100006300277396, 0, NULL, NULL, 'Test User', '2013-09-07 06:49:53', 'CAAHrxXWCZBwsBAEtkOVHkTHC5bMPlr6Q9IbWRSCBLzfEZBmSwKZAtr9ebHCxjO4xStcg9tew90u9fhVl2cTcIx7za6nJYz4wuMQmx7UrQDQntT1nGVOFy87ctuDAHS1jQueV0KXC3MvkWGZCn9ccprzJ8w4pKP0ZD', NULL, 'http://graph.facebook.com/100006300277396/picture?type=large&height=324&width=580', 'facebook', NULL, 100006300277396, '2013-07-09 10:33:49', 'TESTUSER')

Started GET "/users/100006300277396" for 88.248.139.159 at 2013-07-09 15:32:14 +0000
Processing by UsersController#show as HTML
  Parameters: {"id"=>"100006300277396"}
  User Load (130.8ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 100006300277396 LIMIT 1
Completed 404 Not Found in 170ms

ActiveRecord::RecordNotFound (Couldn't find User with id=100006300277396):
  app/controllers/users_controller.rb:17:in `show'

在控制台中,它表示用户的ID为100006300277396,但在数据库中,它设置为2147483647.

我发现MYSQL语句运行良好,但由于ID之前是自动递增的,它会将最后一个自动递增的值插入数据库。因此,问题在于数据库,你看到有什么错误吗?

1 个答案:

答案 0 :(得分:0)

将INT(11)设置为BIGINT(11)以解决此问题