Recall & Review
beginner
What is raw SQL in Rails?
Raw SQL means writing plain SQL queries directly instead of using Rails' ActiveRecord methods. It lets you run custom database commands when needed.
Click to reveal answer
beginner
Why might you use raw SQL in a Rails app?
You use raw SQL when ActiveRecord can't express a complex query easily or when you want better performance by writing optimized SQL.
Click to reveal answer
intermediate
How do you safely include user input in raw SQL queries in Rails?
Use parameter binding with question marks or named placeholders to avoid SQL injection. For example: `User.where("name = ?", user_name)`.
Click to reveal answer
intermediate
What method lets you run raw SQL directly in Rails models?
You can use `ActiveRecord::Base.connection.execute(sql)` to run raw SQL commands directly on the database.
Click to reveal answer
beginner
What is a risk of using raw SQL without care?
If you insert user input directly into raw SQL, it can cause SQL injection attacks, which let attackers access or damage your data.Click to reveal answer
Which Rails method safely inserts user input into raw SQL queries?
✗ Incorrect
Using question marks (?) or named placeholders safely binds parameters and prevents SQL injection.
What does `ActiveRecord::Base.connection.execute(sql)` do?
✗ Incorrect
This method runs the given raw SQL string directly on the database.
When should you prefer raw SQL over ActiveRecord methods?
✗ Incorrect
Raw SQL is best for complex queries or performance tuning not easily done with ActiveRecord.
What is a major security risk of careless raw SQL use?
✗ Incorrect
Inserting user input directly into SQL can allow attackers to run harmful commands.
Which is NOT a benefit of using raw SQL in Rails?
✗ Incorrect
Raw SQL is not always easier; ActiveRecord is simpler for most common queries.
Explain when and why you would use raw SQL in a Rails application.
Think about complex queries and security.
You got /4 concepts.
Describe how to safely include user input in raw SQL queries in Rails.
Focus on security best practices.
You got /3 concepts.