Review
Topics Covered: Modeling models; model relationships.
Office hours
Exit tickets
Exercises
E1: ActiveQuiz
The quizzes will start getting a bit harder! Assume that you'll have to Google for the answers to some of the questions, and make sure you understand them on your own. Put this in d6/active_quiz.txt
.
- What is a database?
- What is a database table? How does it relate to a database? What's a good analogy for a database table (i.e. a way to visualize it)?
- Draw the
tweets
database table
- Why do we use models/resources at all?
- What is ActiveRecord?
- What's the difference between a database table, resource and an ActiveRecord model?
Hover for answer
1. A database is a file on your computer that stores the literal 0s and 1s that represent your data.
2. A database table is a section of the database that corresponds to a particular resource, or logical entity (for example, tweets). We can think of a table like an Excel spreadsheet, with each column being an _attribute_ (like the body of the tweet), and each row being an _instance_ (one particular tweet)
3. Should look like a spreadsheet with three columns: ID, handle and body. Should have three rows with IDs 1, 2, 3 and some handles and bodies in the corresponding columns.
4. We use them to help us reason and think about our database. It's hard to always keep the spreadsheet in our heads, and it makes more sense to us to think about it as one class that has methods. **This step, from database to model, was very important in making web development easier and more accessible**.
5. ActiveRecord is a module that is included in Rails. It makes creating models easier because when a model extends `ActiveRecord::Base` (which is a class—confusing, we know), it gets all the methods in E1 and more for free!
6. A database table just contains 0s and 1s. An ActiveRecord model is a class, with methods and all, that's created in `app/models`. A resource is a model along with its controller and views. You can think of it as the conceptual or logical entity that represents the model. The model is the actual code that communicates with the database. This is **very difficult** to keep track in your head, so you may have to revisit these concepts as you learn more of Rails.
E2: Mo' models
For this exercise, work in your groups or individually. Create a file in d6
called more_models.txt
. You'll go through the full process of modeling Amazon.com, or a similar shopping application. Assume at the very least that there are users who have accounts, and they can add items to their shopping carts. We'll follow the model from in class:
- What are the relevant views or pages? Come up with at least 7.
- What data do we want to store? List all the information we could ever want to store (for example, "items that are in a user's shopping cart"). Come with at least 10.
- What actions can be done? Try to think in terms of who or what does what action to what data ("A user creates a tweet"). Also think CRUD!
- Can you group your data into models (i.e. Tweet or User)? What are those models? A great starting place to think up of models are the actors or actees of your actions (in the above example a user and a tweet). Note that there are often no "right" answers to these questions—it's up to you, the designer, to determine how your app works. What are the attributes (name, handle, body, price, etc.) of each model? How do you decide what's an attribute and what's another model entirely?
- What are the relationships between the models? Note which ones "belong to" others, "have many" others, or "have and belong to many" others. Draw a model diagram like we did in class.
- Finally, draw the tables for each model, including all of its attributes. Include any join tables you have as well.
Into a Rails app!
Make the above into a Rails app. This is the crucial process of going from designing your application to building your application. Two easy steps:
- In
d6
, create a new Rails app called amazon
.
- Using the
rails generate scaffold
command, create a scaffold for each of your models above. It should have all the attributes that you've listed. For example, if you made a User model, you might type something like rails generate scaffold User name:string ...
, where ...
are all the other properties that the user has.
E3: Into the Deep End: Rails Guides
We know that we're throwing you into the deep end here—a lot of the following stuff will be completely unfamiliar and may be overwhelming. Rails is very complex and at the beginning, you won't know what's going on some, or even most, of the time. That's ok. A part of learning to program is being comfortable with being comfortable. You'll mostly copy along at the beginning, and things will slowly start becoming clear, we promise!
Go to Rails Guides Getting Started. Read Section 2. Complete the guide from 3.2 through section 6 (don't do 6).
Create your blog project inside of your iXperience repository inside your d6
and push it to GitHub just like you did with your quizzes.