We'll be using Storedom for the following challenges.
Write ActiveRecord query statements for the following problems. You're welcome to change the schema to fit your needs. Create a gist to store your solutions.
The goal is to find the most efficient query for each piece of information, and to avoid ruby enumerables in favor of leveraging the query capabilities of SQL and ARel.
group 1
- Find all the items that have the word "elegant" in it. (If "elegant" was not in your seeds, feel free to pick another adjective.)
- Find the 5 biggest orders (i.e. orders with the most items).
- Find the first ten orders sorted by date created.
- Given an item, find all of the users who have ordered that item.
- Find all of the orders created between yesterday and a month ago (you can use
rand(1.year.ago..Time.now)
in your seed file to spread out the dates a bit).
group 2
- Given an item. Associate it with an order without touching an
OrderItem
model. - Find the user who has ordered the most items.
- Set it up so that when you destroy a user, ActiveRecord also destroys all of their orders
- Get the second ten orders sorted by date created.
- Given a collection of items, find all of the users who have ordered any of those items.
group 3
- Find the user who has placed the most orders.
- Given a collection of orders, find the users to whom those orders belong, using only one query to the database.
- Get the first ten users and all of their orders in one ActiveRecord query.
- Get all of the orders that have more than five items. Don't use Ruby.
- Find the most popular items (items which have been ordered the most).