Where Active Record 查询中的 OR 运算符
OR operator inside a Where Active Record Query
因此,在 Where Active Record(AR) 查询中,您可以执行以下操作:
game_stickers.where('stickers.name != ?', 'Ban')
但是如何使用 OR 运算符 来测试多个字符串的匹配,而不用 做类似的事情:
game_stickers.where('stickers.name != ? OR stickers.name != ?', 'Ban', 'Closed')
或 没有 恢复到类似 [见下面的注释]:
game_stickers.where.not(stickers.name: ['Ban','Closed','Block'])
注意:
我不想使用最后一个选项的原因是因为我在查询中使用了一些 joins
和 references
(据我所知)不播放这个选项很好。上下文代码类似于:
game_stickers_and_stickers = game_stickers.includes(:sticker)
game_stickers_and_stickers.where('stickers.name = ?', 'Ban').references(:stickers).where(placement_side: side)
也许您可以就执行此查询的最佳方式提出建议。
注意:在我看来,您想要在这些条件之间使用 AND,而不是 OR。想想看。无论如何,试试这个
game_stickers_and_stickers = game_stickers.includes(:sticker)
game_stickers_and_stickers.where.not(stickers: {name: ['Ban','Closed','Block']}).where(placement_side: side)
该条件片段应转换为 SQL
WHERE stickers.name NOT IN ('Ban', 'Closed', 'Block')
因此,在 Where Active Record(AR) 查询中,您可以执行以下操作:
game_stickers.where('stickers.name != ?', 'Ban')
但是如何使用 OR 运算符 来测试多个字符串的匹配,而不用 做类似的事情:
game_stickers.where('stickers.name != ? OR stickers.name != ?', 'Ban', 'Closed')
或 没有 恢复到类似 [见下面的注释]:
game_stickers.where.not(stickers.name: ['Ban','Closed','Block'])
注意:
我不想使用最后一个选项的原因是因为我在查询中使用了一些 joins
和 references
(据我所知)不播放这个选项很好。上下文代码类似于:
game_stickers_and_stickers = game_stickers.includes(:sticker)
game_stickers_and_stickers.where('stickers.name = ?', 'Ban').references(:stickers).where(placement_side: side)
也许您可以就执行此查询的最佳方式提出建议。
注意:在我看来,您想要在这些条件之间使用 AND,而不是 OR。想想看。无论如何,试试这个
game_stickers_and_stickers = game_stickers.includes(:sticker)
game_stickers_and_stickers.where.not(stickers: {name: ['Ban','Closed','Block']}).where(placement_side: side)
该条件片段应转换为 SQL
WHERE stickers.name NOT IN ('Ban', 'Closed', 'Block')