GithubHelp home page GithubHelp logo

fa17-lab2's Introduction

Rails Decal Fall 2017 Lab 2: Associations

Overview

Welcome to Lab 02 of the Rails Decal! In this lab, you will get practice with:

  • Associations
  • Migrations
  • Routes
  • MVC

Getting Started

Feel free to clone this app and get started from there. Remember to run bundle install. Alternatively, you may also start the app from scratch by doing:

rails new lab2
cd lab2

Models

Step 1 Let's begin by creating two models with the following attributes:

Trainer

name: a string containing the name of the trainer
age: an integer that contains the age of the trainer

Pokemon

name: a string containing the name of the pokemon
attack: an integer representing the attack points of the pokemon

Finally, run rails db:migrate to create your tables.

Step 2 Oh wait! I forgot that pokemon should also have a health column :( Luckily, we have migrations that can help us.

Generate a migration that adds a integer column to the pokemons table that represents the health points of a pokemon.

Hint: Is there a single command you can run in your terminal to generate this for you?

Associations

Let's create the association between pokemon(s) and trainers!

Create the migration that will add the foreign key, trainer_id column to the pokemons table.

Run rails db:migrate and play around in the rails console to create some trainers and their pokemon!

Model-View-Controller

Step 1 Create a trainers controller that contains an index method that will allow you to fetch all trainers from your database.

Hint: This method should contain one line.

Step 2 Create a route to '/' (localhost:3000) for this index method. The view is provided for you in app/views/trainers/index.html.erb if you cloned this repo. Do however take a look the code in the view file to see how things are getting displayed.

Step 3 You should now be able to run rails server and immediately see a list view of all trainers and their pokemon! Congrats!

Seed File

Often times, it isn't very efficient to run rails console in order to create data models when you don't have a form to create them in your web app.

Another way we can quickly populate our database with default values is through a seed file, located in db/seeds.rb. Take a look at the file to see what it does.

Finally, to seed your database, run rails db:seed and refresh the page to see the new data.

fa17-lab2's People

Contributors

nhiquach avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.