Homework for 9B

Review

Topics Covered: Relations; project sketching


Exercises

E1: Relations

When thinking about relations and foreign keys, it often helps to use an anology. Imagine you were your city or town's resident cat lady/cat guy and had decided to adopt 100 cats. Would it be more efficient for you to remember each of your cats, or for each cat simply to remember you? In Rails relations terms, you, as the owner, has_many cats. Each cat belongs_to you. Rather than you having to remember 100 foreign keys, it would make sense for each cat to instead simply store your foreign key.

Above we used the belongs_to relation for our cats. When should we use the has_one relation instead? Is there a difference between the two?

E2: Young Many Cash Many Billionaires

The most technical of the relations is undoubtedly many-to-many. Answer the following questions about the relation and check with a TA when you're done.

  1. What is a many to many relationship and when do we use it?
  2. Give an example in Twitter, Tasks, and MeowBook of a many to many relationship.
  3. What do we change in the models to reflect this relationship?
  4. What do we change about the migrations to reflect this relationship?
  5. What is a join table and why do we use them for many to many relationships? Draw a visual of a join table that could store the many to many relationship for hashtags and tweets.

E3: Mo Verbs Mo Actions

HTTP methods (also called verbs) are used to define actions to be performed on a particular resource. We will be dealing with four main verbs for the majority of this course. Below are brief descriptions of two of the most used verbs. See if you can match the GET and DELETE verbs with the proper definition.

Based on these definitions, which verb do you think you would use for the index and show actions?

We will also be dealing with the PUT and POST verbs in future lectures. Together, these four verbs correspond to create, read, update, and delete (or CRUD) operations. Although there are other verbs, e.g. OPTIONS and HEAD, these are used far more infrequently than the previous four.

E4: Rails Console

  1. Open up IRB or your Rails console.
  2. Do nothing.
  3. You are now a REPL without a cause.