Today I Learned

hashrocket A Hashrocket project

Execute a prepared statement in Rails

The PostgreSQLAdapter in rails has a method called #exec_query that allows you to use prepared statements with bind variables:

User.connection.exec_query(
  "select * from users where email = $1",
  nil,
  ["a@example.com"],
  prepare: true
)

Will issue:

select * from users where email = $1  [[nil, "a@example.com"]]
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.