@usersfound = User.find_by_sql(["
SELECT * from users where name @@ plainto_tsquery('english', ?) LIMIT 20 offset ?
",@query,@offset])
See above, is this safe from sql injection? I am very new to doing direct sql commands on a database in rails. (I am aware there may be other ways of doing this SPECIFIC query, but I am wondering if in general, using find_by_sql and that kind of insertion of vars is safe – I have some difficult queries with subselects and joins that are really possible to do with ActiveRecord.
Thanks.
Yes, that should be safe. If you trace through the code you’ll find that your
find_by_sqlcall ends up callingPGconn#send_query_preparedwith the bind parameters being carried along as little more than baggage; thesend_query_preparedmethod is just a wrapper for thePQsendQueryPreparedAPI call inlibpq:The bind parameters end up in
paramValues. So you should be fine unless there are bugs in PostgreSQL’s C library prepared statement handling.