token.user_id won't save, but only in Rails 3.1 for the consumer #96

yutai opened this Issue Nov 6, 2011 · 4 comments

3 participants


Ok, I know there's already been a few posts on this one already...

I've tried to build the sample provider-consumer from the excellent tutorial

I've followed all the fixes, edits, of that tutorial including the advice to add
def current_user=(user)
current_user = user
to the consumer's application controller

and it works great when running 3.0.7 on the consumer. The provider works regardless of 3 vs 3.1
When running the consumer in 3.1, the user_id keeps saving as nil when saving the token.

Trying to debug into the gem, i found something maddening:

if i go to the token model of the gem,

in the def for

before line 51, which is! if token.new_record? or token.changed?

if i log token.user_id, it outputs the right user id.

but after it saves on line 51, log for token.user_id becomes nil

and then, if i just add two more lines after line 51, where it saved the token,

51! if token.new_record? or token.changed?
52 token.user_id =

magic. it saves the user_id now in the token.

why in the world did it not save it the first time? 

and why does this not happen in 3.0.7?

Thanks for reading this far down my messy post. 


Could the bang be the culprit?


Here is my debugger log that from a Twitter callback:

User in request_token: #<User id: 1, email: "", ...>
User in access_token: #<User id: 1, email: "", ...>
  �[1m�[36mTwitterToken Load (0.3ms)�[0m  �[1mSELECT "consumer_tokens".* FROM "consumer_tokens" WHERE "consumer_tokens"."type" IN ('TwitterToken') AND "consumer_tokens"."user_id" = 1 AND "consumer_tokens"."token" = '[REDACTED]' LIMIT 1�[0m
Token before save: #<TwitterToken id: nil, user_id: 1, type: "TwitterToken", ...>
  �[1m�[35mCACHE (0.0ms)�[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  �[1m�[35m (0.1ms)�[0m  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1
  �[1m�[36mSQL (11.1ms)�[0m  �[1mINSERT INTO "consumer_tokens" ("created_at", "secret", "token", "type", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?)�[0m  [["created_at", Mon, 07 Nov 2011 02:14:25 UTC +00:00], ["secret", "[REDACTED]"], ["token", "[REDACTED]"], ["type", "TwitterToken"], ["updated_at", Mon, 07 Nov 2011 02:14:25 UTC +00:00], ["user_id", nil]]
Token after save: #<TwitterToken id: 25, user_id: nil, type: "TwitterToken", ...>
Token after update: #<TwitterToken id: 25, user_id: 1, type: "TwitterToken", ...>
  �[1m�[35mUser Load (0.3ms)�[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  �[1m�[36m (0.5ms)�[0m  �[1mUPDATE "consumer_tokens" SET "user_id" = 1, "updated_at" = '2011-11-07 02:14:25.032692' WHERE "consumer_tokens"."type" IN ('TwitterToken') AND "consumer_tokens"."id" = 25�[0m
Redirected to http://...

Something goes wrong during the INSERT that @yutai overwrites with an UPDATE...


Solved: Probable Devise method conflict - Rename/replace create_user

require 'oauth/models/consumers/token'

class ConsumerToken < ActiveRecord::Base
  include Oauth::Models::Consumers::Token

  # You can safely remove this callback if you don't allow login from any of your services
  before_create :make_user

  # Modify this with class_name etc to match your application
  belongs_to :user


  def make_user
    self.user ||= begin {} => false)

Wow, great find! Been struggling with the same issue for a few hours now. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment