Homework for 8B

Exercises

E1: Conceptual Review

Migrations

  1. At a high level, what is a migration?
  2. What is the code to generate a migration?
  3. What is the code to run a migration?
  4. If I just generated a migration create_stories, next added a new field t.string:author in my migration file, and then ran the migration, what does it to do my table?
  5. What happens if you add fields to a migration after you run it? Are those change reflected on the database table? Why or why not?

Seeds

  1. What is a seeds file?
  2. Where does it live?
  3. What is it good for?
  4. How do I run it?

E2: Rebuild

Let's walk through building our own full CRUD app, but instead of Twitter, we'll do it for Reddit—without the API.

Remember the steps we did in class, and think about what lines of code go with each step:

  1. Create a new Rails project called reddit_by_hand
  2. Go into the directory
  3. Start the server to make sure things are working.
  4. Create a route for a page that I want
  5. "Follow the errors" until the page works :)

Following the Errors

Like mentioned in lecture, it doesn't really matter what you create first: routes, models, migrations, controllers, or views. But at the infancy of your coding career, errors help guide you step-by-step in decide what to build. So if you see uninitialized constant StoriesController, try solving that. Then you'll get another error, so solve that. And so on.

The Way from Class

Without data or models:

  1. Create a controller
  2. Create an action inside that controller
  3. Create a view for that action

With data and models:

  1. Create a model
  2. Create a migration that makes a table for that model
    • Add the necessary properties to the migration
  3. Run the migration
  4. Hook up the data between your view and your model through your controller

Step 4 might look something like:

# In the controller
def action_name
  @models = Model.get_stuff
end

# In the view
# ...show @models in some way

Building Reddit by Hand

Go through the above steps (based off what we did in class) to create an app called reddit_by_hand. Give it functionality so that, using the application via the web browser, you can view all stories, create a new story and show a single story (this is new!).

BEFORE YOU START, think:

This is why MVC is called MVC (but maybe should be called MVCr to incorporate routes). It's a way of structurally thinking about your application in an organized, step-by-step way. Hopefully things are slowly coming into focus!

Only now that you've designed your application, you should start building it. Godspeed.

E3: Even Mo' Models

If you didn't in the exercise above, create a User model and a Category model, and migrations for each of them. What attributes will each have and what are their types? Give each Story a user_id and a category_id. Make sure to create a new migration, don't edit the one you already created!

In your seeds.rb file, create a user, a category, and then 5 stories that belong to that category and user. Edit your view to show the user_id and category_id of each story.