title: Invalid syntax for integer
body: <p>I'm receiving this error message when trying to match up integer ID's in my query us Postgres.</p>
  
  <pre><code>PG::InvalidTextRepresentation:ERROR: invalid input syntax for integer: "matches.team_id"
  </code></pre>
  
  <p>I have associations in place.</p>
  
  <p><strong>Stat</strong></p>
  
  <pre><code>belongs_to :player
  has_many :matches, foreign_key: 'match_id', primary_key: 'match_id'
  </code></pre>
  
  <p><strong>Match</strong></p>
  
  <pre><code>has_many :stats
  has_many :players, through: :stats
  </code></pre>
  
  <p><strong>Stats Controller</strong></p>
  
  <pre><code>@player = Player.find(params[:id])    
  @stats = Stat.where("player_id = ?", @player.id).joins(:matches)
  .where('stats.team_id = ?', 'matches.team_id').select('SUM(stats.goals) AS goals')
  </code></pre>
  
  <p>My Matches database has two <code>match_id</code> rows that are the same and when I sum out the players goals it finds the players goals, but doubles the <code>goals</code> when displaying. So I figured if my query stated that only find where team_id's are the same then it would only generate 1 goal per <code>match_id</code></p>
  
  <p><strong>Matches db</strong></p>
  
  <pre><code>|id|match_id|team_id|total_score|
  |10|  3433  |   1   |     3     |
  |11|  3433  |   2   |     2     |
  </code></pre>
  
  <p><strong>Stats db</strong></p>
  
  <pre><code>|id|match_id|team_id|player_id|goals|
  |12|  3433  |   1   |   333   |  1  |
  </code></pre>