Sample solutions to exercises can be found here: https://gist.github.com/kjs222/5169a292d87d1774832b7160f7ba2b49
###Setup:
- Clone Repo:
git clone [email protected]:turingschool-examples/storedom.git
bundle update
-
Change database from sqlite to Postgres (requires changes to gemfile and database.yml). Bundle again.
-
Change the seed file to have random created_at timestamps for date related queries. Users and Items should have a created_at date between 200 and 1000 days ago. Orders between today and 200 days ago.
created_at: rand(200..1000).days.ago
4. Set up the database:
rake db:setup
From here on out, you will be working directly in the postgres database. Type: 'psql ` to access your database. Your database name will be in your database.yml file.
NOTE: After I did these exercises in Postgres, I went back and tried to do most of them in ActiveRecord as well.
###Exercises:
-
Add column expiration_date to items as a timestamp. Do not allow null values. Set a default value to one year after the date the item was created.
-
Fix your existing records to have an expiration date that is 1 year after their created_at date.
-
Change expiration date field to be unix time rather than a timestamp. Do not delete/drop any records, tables, etc. Do not use created_at date to recalculate. Work with your current expiration date column and find a way to transition that data to the new format.
-
The items table currently has a cost field, but there are no values in any of the records. Populate this field for all records with a random value that reprsents a cost between $0.00 and $10.00. Note the data type.
-
Use the cost data you just created to populate the amount field in orders. The amount in orders should be the sum of the costs of the order_items in that order.
-
Create a new table called categories with an id field as the primary key and a name field that is 40 characters or less and cannot be null.
-
Add some constraints on the category table. First, make sure the name is unique. Next, make sure the name only consists of upper or lower case letters.
-
Add a category with a name of "MISC". Try adding a few other categories that violate your constraints to make sure they are working as you intended.
-
Add category_id as a foreign key on the items table. Do not allow null values and default the value to be the MISC category you just made.
-
Add another category. Then change items with IDs between 10 and 50 to have that category id.
-
Add another category. Then change items with names that begin with 'A' to have that category id.
-
Find all orders that were placed on a Tuesday.
-
Find all customers who placed orders on a Tuesday.
-
Find all customers who placed orders on Tuesday where the order amount was greater than $50. Display the user's name and the order amount in a currency format like $50.00.
-
Find all items that have not been ordered.
-
Get a list of users who created an account between Jan 1 2014 and Jan 1 2015
-
Find the earliest date when a user created an account.
-
Find the name of the first user to create an account and the last user to create an account
-
Find the id and name of the first user to place an order with an item from the category "MISC"
-
Find the total dollar amount of all of user 1's orders. Display the total in currency format like $100.00.
-
Display the id, name, and total order amount (in currency format) for all users, ordered by highest total order amount to lowest.
-
Display the item id, item name, and expiration date (as a timestamp) for all items that are expiring within the next 30 days. Order from nearest expiration date.
-
Do the same query as above, but display expiration date as days until expiration instead of a timestamp.