Project

General

Profile

Defect #42622

Updated by Marius BĂLTEANU 6 days ago

When using JOINs with the User model, if both @atom_token@ and @api_token@ are utilized like this: 

 <pre><code class="ruby"> 
 User.joins(:atom_token, :api_token) 
 </code></pre> 


 then this error results: 
 <pre><code class="ruby"> 
 #<ActiveRecord::StatementInvalid:"PG::AmbiguousColumn: ERROR:    column reference \"action\" is ambiguous\nLINE 1: ... \"api_tokens_users\".\"user_id\" = \"users\".\"id\" AND (action='ap...\n 
 </code></pre> 

 This can be fixed by prepending the proper table name in the `where` clause on these lines, 92 and 93 in user.rb: 
 <pre><code class="ruby"> 
   has_one :atom_token, lambda {where "#{table.name}.action='feeds'"}, :class_name => 'Token' 
   has_one :api_token, lambda {where "#{table.name}.action='api'"}, :class_name => 'Token' 
 </code></pre> 
 (Note that all that is being added here is @#{table.name}.@) `#{table.name}.`) 

 When doing this then the resulting SQL turns into: 
 <pre><code class="ruby"> 
 SELECT "users".* FROM "users" INNER JOIN "tokens" ON "tokens"."user_id" = "users"."id" AND (tokens.action='feeds') INNER JOIN "tokens" "api_tokens_users" ON "api_tokens_users"."user_id" = "users"."id" AND (api_tokens_users.action='api') WHERE "users"."type" = $1 
 </code></pre> 

 I have no idea how to create a pull request with SVN to provide this fix, so if someone could do this then awesome :)

Back