Recall & Review
beginner Click to reveal answer
What does the
group method do in Rails ActiveRecord?The
group method groups query results by one or more columns, similar to SQL's GROUP BY. It helps organize data into sets based on shared values.intermediate Click to reveal answer
How do you use
having in Rails ActiveRecord queries?You use
having to filter groups created by group. It works like SQL HAVING, letting you specify conditions on aggregated data.beginner Click to reveal answer
Example: How to find authors with more than 3 books using
group and having?Use
Author.joins(:books).group(:id).having('COUNT(books.id) > 3'). This groups authors by id and filters those with more than 3 books.intermediate Click to reveal answer
Why use
having instead of where after grouping?where filters rows before grouping. having filters groups after aggregation. Use having to filter based on aggregate results.intermediate Click to reveal answer
Can you chain
group and having with other ActiveRecord methods?Yes, you can chain
group and having with select, order, and joins to build complex queries.What does
group(:category) do in a Rails query?✗ Incorrect
group(:category) groups the query results by the category column, like SQL GROUP BY.Which method filters groups after aggregation in Rails?
✗ Incorrect
having filters groups after aggregation, unlike where which filters rows before grouping.How to find users with more than 5 posts using ActiveRecord?
✗ Incorrect
Use
group and having to filter users by aggregated post counts.Can
having be used without group in Rails?✗ Incorrect
having filters groups, so it needs group to work.What SQL clause does Rails
having correspond to?✗ Incorrect
Rails
having corresponds directly to the SQL HAVING clause.Explain how to use
group and having together in Rails ActiveRecord queries.Think about grouping data first, then filtering those groups.
You got /4 concepts.
Describe a real-life scenario where you would use
group and having in a Rails app.Imagine sorting and filtering collections of items by counts or sums.
You got /3 concepts.
