当我尝试在我的 Postgres 查询中匹配整数 ID 时收到此错误消息。
PG::InvalidTextRepresentation:ERROR: invalid input syntax for integer: "matches.team_id"
我有适当的关联。
统计
belongs_to :player
has_many :matches, foreign_key: 'match_id', primary_key: 'match_id'
匹配
has_many :stats
has_many :players, through: :stats
统计 Controller
@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')
我的比赛数据库有两个相同的 match_id
行,当我计算球员进球时,它会找到球员进球,但在显示时加倍 goals
。所以我想如果我的查询声明只找到 team_id 相同的地方,那么它只会为每个 match_id
匹配数据库
|id|match_id|team_id|total_score|
|10| 3433 | 1 | 3 |
|11| 3433 | 2 | 2 |
统计数据库
|id|match_id|team_id|player_id|goals|
|12| 3433 | 1 | 333 | 1 |
最佳答案
改变这个:
where('stats.team_id = ?', 'matches.team_id')
为此:
where('stats.team_id = matches.team_id')
当您使用 ?
替换时,您是说 ?
的数据应该被转义。它正在查看 'matches.team_id'
的类型,发现它是一个字符串,并将其用引号引起来。
可能有一种方法可以在不需要自定义 where
子句的情况下进行连接,但问题的根源在于字符串转义机制。
关于ruby-on-rails - 整数语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40622875/