ruby-on-rails - 整数语法无效

标签 ruby-on-rails postgresql ruby-on-rails-4

当我尝试在我的 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

生成 1 个目标

匹配数据库

|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/

相关文章:

css - @font-family Rails 中的 CSS 规范,未显示正确的字体

ruby-on-rails - 如何正确检查 ruby​​ 中的嵌套值

postgresql - Postgres 表似乎部分丢失

ruby-on-rails - rails : is Passenger Standalone suitable for production deployment?

sql - PostgreSQL:如何从数据输出中编辑数据?

sql - 合并具有不同列的 PostgreSQL 查询

ruby-on-rails - 检查 Postgres 数组中是否存在任何给定的值数组

ruby-on-rails-4 - Rails 4.2 服务器;私有(private)和公共(public)IP不工作

ruby-on-rails - 通过系统调用在 ruby​​ 中嵌套超时的问题

ruby-on-rails - Twitter 的 Omniauth 服务不可用错误