ActiveRecord 查询从 Rails 2 到 Rails 4.2 的转换
ActiveRecord Query Conversion from Rails 2 to Rails 4.2
我习惯了Rails2并且有一个现有的查询:
@players = Player.find(
:all,
:select => 'players.*, positions.name AS "position_name"',
:joins => 'LEFT JOIN positions ON positions.id = players.position_id',
:conditions => ['status IN (?, ?, ?, ?)', 'd', 'i', 'n', 'u']
)
我正在尝试编写 Rails 4 等效项,但出现错误 unexpected ')', expecting keyword_end
,这是查询的右括号。
@players = Player
.select('players.*, positions.name AS "position_name"')
.joins('LEFT JOIN positions ON positions.id = players.position_id')
.where(status: ['d', 'i', 'n', 'u'])
)
我已尝试阅读文档,但看不出哪里出错了。
此外,如果这是使用 params
散列而不是硬编码值,上面是否会受到 sql 注入的影响,如果是这样,如何使用占位符编写它,例如第一个 Rails 2 查询?
删除最后一个)
;这是新查询结构的额外内容。
@players = Player
.select('players.*, positions.name AS "position_name"')
.joins('LEFT JOIN positions ON positions.id = players.position_id')
.where(status: ['d', 'i', 'n', 'u'])
将被视为没有新行的以下内容:
@players = Player.select('players.*, positions.name AS "position_name"').joins('LEFT JOIN positions ON positions.id = players.position_id').where(status: ['d', 'i', 'n', 'u'])
我习惯了Rails2并且有一个现有的查询:
@players = Player.find(
:all,
:select => 'players.*, positions.name AS "position_name"',
:joins => 'LEFT JOIN positions ON positions.id = players.position_id',
:conditions => ['status IN (?, ?, ?, ?)', 'd', 'i', 'n', 'u']
)
我正在尝试编写 Rails 4 等效项,但出现错误 unexpected ')', expecting keyword_end
,这是查询的右括号。
@players = Player
.select('players.*, positions.name AS "position_name"')
.joins('LEFT JOIN positions ON positions.id = players.position_id')
.where(status: ['d', 'i', 'n', 'u'])
)
我已尝试阅读文档,但看不出哪里出错了。
此外,如果这是使用 params
散列而不是硬编码值,上面是否会受到 sql 注入的影响,如果是这样,如何使用占位符编写它,例如第一个 Rails 2 查询?
删除最后一个)
;这是新查询结构的额外内容。
@players = Player
.select('players.*, positions.name AS "position_name"')
.joins('LEFT JOIN positions ON positions.id = players.position_id')
.where(status: ['d', 'i', 'n', 'u'])
将被视为没有新行的以下内容:
@players = Player.select('players.*, positions.name AS "position_name"').joins('LEFT JOIN positions ON positions.id = players.position_id').where(status: ['d', 'i', 'n', 'u'])