Complete the code to execute a raw SQL query using ActiveRecord.
User.find_by_sql("SELECT * FROM users WHERE id = [1]")
The raw SQL query requires a specific value for the user ID, so '1' is the correct choice.
Complete the code to safely insert a variable into a raw SQL query using ActiveRecord.
User.find_by_sql(["SELECT * FROM users WHERE email = ?", [1]])
When using parameterized queries, the value must be a string with quotes, so '"user@example.com"' is correct.
Fix the error in the raw SQL query that fetches users with age greater than 30.
User.find_by_sql("SELECT * FROM users WHERE age [1] 30")
The query should select users older than 30, so the '>' operator is correct.
Fill both blanks to write a raw SQL query that selects users with name starting with 'A' and orders by created_at descending.
User.find_by_sql("SELECT * FROM users WHERE name [1] 'A%' ORDER BY created_at [2]")
Use 'LIKE' to match names starting with 'A' and 'DESC' to order newest first.
Fill all three blanks to write a raw SQL query that updates the user's email where id matches, using parameterized query.
User.connection.execute(ActiveRecord::Base.send(:sanitize_sql_array, ["UPDATE users SET email = [1] WHERE id = [2]", [3]]))
The email is a string, so use a quoted string for the first blank. The id is a number, so use 42 for the second and third blanks (the id value).