Remove repetitions from the Authorised Applications view

Hi guys,

I was having a number of repeated applications being shown on the authorised applications list (/oauth/authorized_applications) because Application.authorized_for doesn't return the unique list of authorisations. 

Assuming there's a client application already registered on the provider, here is the flow:
1) User creates account on the provider
2) User tries to login on a client application
3) User is redirected to the provider, where he/she login (if not logged in already, otherwise skips the step)
4) User authorise the application to access the private data
5) User gets redirected to the client app with a valid token.

Now if the user leaves the client application page and later on tries to login again, then steps 2, 3 and 5 happen. If the user now goes to the provider and tries to access /oauth/authorized_applications, there are 2 entries even though they represent the same authorisation...

Here is the problem:

1) Get the resource owner:

ruby-1.9.3-p0 :031 > resource_owner = User.find(14)  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 14]] 
=> \#\<User id: 14, email: "u@u.com", encrypted_password: "$2a$10$6v2l2w0iNKll7L9qItfTtOzzbHt7NsyK0CvJ43bvwT4Q...", remember_created_at: nil, created_at: "2012-01-10 10:09:41", updated_at: "2012-01-10 10:09:41", first_name: "U", last_name: "U", invitation_token: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil\>

2) Fetch the authorisations for the given resource owner:

ruby-1.9.3-p0 :032 > authorized_apps = Application.authorized_for(resource_owner)
  Application Load (0.2ms)  SELECT "oauth_applications".* FROM "oauth_applications" INNER JOIN "oauth_access_tokens" ON "oauth_access_tokens"."application_id" = "oauth_applications"."id" AND "oauth_access_tokens"."revoked_at" IS NULL INNER JOIN "oauth_applications" "authorized_applications_oauth_applications" ON "authorized_applications_oauth_applications"."id" = "oauth_access_tokens"."application_id" WHERE "oauth_access_tokens"."resource_owner_id" = 14
 => [\#\<Application id: 3, name: "Dsci omniauth test", uid: "a24d0f9b08fe504970fae9572171119c11a3b432fd0c33c8b58...", secret: "93c7ec4f9c85fac2320595e9bf038999b6e5ef1639bede11046...", redirect_uri: "http://localhost:9999/auth/dsci/callback", created_at: "2012-01-09 13:06:34", updated_at: "2012-01-09 13:06:34">, \#\<Application id: 3, name: "Dsci omniauth test", uid: "a24d0f9b08fe504970fae9572171119c11a3b432fd0c33c8b58...", secret: "93c7ec4f9c85fac2320595e9bf038999b6e5ef1639bede11046...", redirect_uri: "http://localhost:9999/auth/dsci/callback", created_at: "2012-01-09 13:06:34", updated_at: "2012-01-09 13:06:34"\>]

3) Get unique authorisations

ruby-1.9.3-p0 :033 > authorized_apps.uniq
 => [\#\<Application id: 3, name: "Dsci omniauth test", uid: "a24d0f9b08fe504970fae9572171119c11a3b432fd0c33c8b58...", secret: "93c7ec4f9c85fac2320595e9bf038999b6e5ef1639bede11046...", redirect_uri: "http://localhost:9999/auth/dsci/callback", created_at: "2012-01-09 13:06:34", updated_at: "2012-01-09 13:06:34"\>]


What do you think? Have you encountered this problem?

Thanks,
  Carlos