We know how to create an MVC app, and we've discovered ActiveRecord, which allow us to manipulate records and associations in the database really easily. For the next lab session, we're going to build a Cookbook app:
By the end you should be able to create records and associations in the Rails console bin/rails c
. Generate and run the migrations and create the models with all the associations so that it is possible to link the models in the console.
Remember that you can specify references
when creating models, this way the migration also generates foreign keys which are needed to build the relationships between tables.
# This command assumes there is a manufacturers table
# This command generates a new Car model
# The generated migration includes the color, mpg, and manufacturer_id columns
rails g model Car color:string mpg:integer manufacturer:references
-
Choose the appropriate data types for each column.
-
Create the following models:
- Course
- attributes: name, description
- Recipe
- attributes: name, servings, description, directions, course_id (foreign_key)
- RecipeIngredient
- attributes: ingredient_id (foreign_key), recipe_id (foreign_key)
- Ingredient
- attributes: name, description
- Course
-
The associations are as follows:
- A course has many ingredients through recipes
- A course has many recipes
- A recipe belongs to a course
- A recipe has many recipe_ingredients
- A recipe has many ingredients through recipe_ingredients
- A recipe_ingredient belongs to ingredient
- A recipe_ingredient belongs to recipe
- An ingredient has many recipes through recipe_ingredients
- An ingredient has many recipe_ingredients
- Follow these steps to generate your starter code:
rails new cookbook_app -d postgresql -T
cd cookbook_app
bundle exec rake db:create
- Add the following gems and then bundle:
gem "awesome_print"
- You are now ready to start creating models!
In the rails console rails c
, you should be able to type the following commands:
# Create the first course
course1 = Course.create(name: "dinner party", description: "yummy dinner")
# Create a new recipe for our first course
course1.recipes.create(name: "cheesecake", description: "sweet and salty caramel cheesecake", servings: 6, directions: "shake and bake with love")
# Add the necessary ingredients for the first recipe
course1.recipes.first.ingredients.create(name: "cream cheese", description: "made from organic farm raised milk")
# Create a second recipe for our first course
course1.recipes.create(name: "cheese burger", description: "american staple burger")
# Add the necessary ingredients for the second recipe
course1.recipes.second.ingredients.create([{name: "ground meat"}, {name: "american cheese"}, {name: "lettuce"}])
# Let's update our instance of the course
course1.reload
# Get all recipes for the first course
course1.recipes # MUST output 2 recipes!
# Get all ingredients for the first course (includes all ingredients for both recipes)
course1.ingredients # MUST output 4 ingredients!
If you can get the same results returned when typing these commands, you have earned your Associations Private First Class Insignia. TAKE a screenshot of the results and slack them to me.
- A link to the official associations docs