Home>

Using Rails find_by_sql
SomeModel.find_by_sql (["SELECT 1 FROM ip_addrs WHERE ip_addr=': FFFF' AND id=: id", {id: 1}])
I want to execute a case that includes a colon in SQL and replaces the parameter name, but the colon in SQL is regarded as a replacement and an error occurs.

Isn't there an escape method that distinguishes between a normal colon and a colon for replacement?

{id: 1, FFFF:': FFFF'} It's an image that seems to be the only solution. ..

Yuki Inoue♦2021-03-05 20:33:28

Since the ip_addr part has combinations from 0000 to FFFF, you have to describe 65536 different keys ...

gocho2021-03-05 20:33:28

Does that mean that you don't want to extract with a specific ip_addr, but that ip_addr can change? If so, it would be treated as a variable and passed as a hash ...

nekketsuuu♦2021-03-05 20:33:28

It is assumed that the number of ip_addr to be described changes dynamically (ip_addr, ip_addr_from, ip_addr_to) There are multiple pairs, and it is a case to make sure that all pairs satisfy ip_addr BETWEEN ip_addr_from AND ip_addr_to.

gocho2021-03-05 20:33:28

Can't it be realized with a placeholder that uses? SomeModel.find_by_sql (["SELECT 1 FROM ip_addrs WHERE ip_addr=': FFFF' AND id=?", 1])

sugumura2021-03-05 20:33:28