pelle/oauth-plugin — Issue #96: token.user_id won't save, but only in Rails 3.1 for the consumer
Author: yutai
Created: 2011-11-06 00:20:34+00:00
State: open

Ok, I know there's already been a few posts on this one already...
https://github.com/pelle/oauth-plugin/issues/88
https://github.com/pelle/oauth-plugin/issues/84

I've tried to build the tutorial provider-consumer from the excellent tutorial
http://unhandledexpression.com/2011/06/02/rails-and-oauth-plugin-part-1-the-provider/
http://unhandledexpression.com/2011/06/28/rails-and-oauth-plugin-part-2-the-consumer/

I've followed all the fixes, edits, of that tutorial including the advice to add
def current_user=(user)
current_user = user
end
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,
https://github.com/pelle/oauth-plugin/blob/master/lib/oauth/models/consumers/token.rb

in the def for
```find_or_create_from_access_token```

before line 51, which is
```
token.save! 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 token.save! if token.new_record? or token.changed?
52 token.user_id = user.id
53 token.save
```
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.