Code Monkey home page Code Monkey logo

week_05-day_01-rails-one-to-many's Introduction

General Assembly Logo

Rails One to Many

Let's build a rails application!

Objectives

By the end of this, developers should be able to:

  • Diagram the database tables and Entity Relationship Diagram that describe a one-to-many relationship.
  • Create a new resource using scaffold.
  • Write a migration for a one-to-many relationship.
  • Compare has_many and belongs_to to other macros, like attr_accessor.
  • Configure ActiveRecord to manage one-to-many relationships using has_many and belongs_to.
  • Create associated records using the rails console.

Steps for Adding a Relationship to a Model

  1. Generate the new resource it will have a relationship with if needed
  2. Add a foreign key to the resource that belongs to the other resource
  3. Add relationship macros to both models
  4. Update controllers
  5. Update views

Methodical, Error-Driven Development

Status checks will be frequent. As developers we want to be meticulous and make sure we're getting errors where expected as we build our server.

This is called "error-driven development". The goal is to see an error, and to learn what error to expect. Embrace the errors, and they will tell you what your next task is.

Follow the steps outlined in good Error Driven Development

  1. Route
    • Test the route, see that a route does not exist
    • Add the route
    • Test the route, see that a route does exist
  2. Controller
    • Test the route, see that a route does exist but controller does not
    • Add the controller
    • Test the route, see that a controller exists
  3. Model
    • Test the route, see that a controller exists but model does not
    • Add the model
    • Test the route, see that a Model exists
  4. Migrations
    • Test the route, see that a Model exists but migrations must be run
    • Run migrations
  5. View
    • Test the view, see that it does not exist
    • Add the view
  6. Test Feature
    • Test the route, ensure actions are successful
    • Use Rails Console to ensure all data persists as expected

Demo: Books

User Stories

  • Version 1:

    • As a user, I want to view a single book
    • As a user, I want to view all books
    • As a user, I want to create a book with a title and author
    • As a user, I want to edit a book's title and author
    • As a user, I want to delete a book
  • Version 2:

    • As a user, I want to view a single book
    • As a user, I want to view all books
    • As a user, I want to create a book with a title
    • As a user, I want to edit a book's title
    • As a user, I want to delete a book
    • As a user, I want to view an author
    • As a user, I want to view all authors
    • As a user, I want to create an author with a given name and family name
    • As a user, I want to edit an author's given name and family name
    • As a user, I want to delete an author
    • As a user, I want to assign a single author to a book
    • As a user, I want to assign multiple books to an author

Entity Relationship Diagram (ERD)

Version 1

Books
id primary key
title string
author string
created_at datetime
updated_at datetime

Version 2

Author has many Books

Books belong to Author

Authors -|--< Books

Books Authors
id primary key id primary key
title string first_name string
author_id foreign key last_name string
created_at datetime created_at datetime
updated_at datetime updated_at datetime

Code Along: Hospital

User Stories

  • Version 1

    • As a user, I want to view a single patient
    • As a user, I want to view all patients
    • As a user, I want to create a patient with a first name, last name, diagnosis and born on
    • As a user, I want to edit a patient's first name, last name, diagnosis and born on
    • As a user, I want to delete a patient
  • Version 2

    • As a user, I want to view a single patient
    • As a user, I want to view all patients
    • As a user, I want to create a patient with a first name, last name, diagnosis and born on
    • As a user, I want to edit a patient's first name, last name, diagnosis and born on
    • As a user, I want to delete a patient
    • As a user, I want to view a doctor
    • As a user, I want to view all doctors
    • As a user, I want to create a doctor with a given name, family name, zip code and specialty
    • As a user, I want to edit a doctor's given name, family name, zip code and specialty
    • As a user, I want to delete a doctor
    • As a user, I want to assign a single doctor to a patient
    • As a user, I want to assign multiple patients to a doctor

Entity Relationship Diagrams

Version 1

Patients
id primary key
first_name string
last_name string
diagnosis string
born_on date
created_at datetime
updated_at datetime

Version 2

Doctor has many Patients

Patients belong to Doctor

Patients -|--< Doctors

Patients Doctors
id primary key id primary key
first_name string first_name string
last_name string last_name string
diagnosis string zip_code string
born_on date specialty string
doctor_id foreign key created_at datetime
created_at datetime updated_at datetime
updated_at datetime

Lab: Cafeteria

User Stories

  • Version 1

    • As a user, I want to view a single ingredient
    • As a user, I want to view all ingredients
    • As a user, I want to create an ingredient with name and unit
    • As a user, I want to edit an ingredient's name and unit
    • As a user, I want to delete an ingredient
  • Version 2

    • As a user, I want to view a single ingredient
    • As a user, I want to view all ingredients
    • As a user, I want to create an ingredient with name and unit
    • As a user, I want to edit an ingredient's name and unit
    • As a user, I want to delete an ingredient
    • As a user, I want to view a recipe
    • As a user, I want to view all recipes
    • As a user, I want to create a recipe with a name and description
    • As a user, I want to edit a recipe's name and description
    • As a user, I want to delete a recipe
    • As a user, I want to assign a single recipe to an ingredient
    • As a user, I want to assign multiple ingredients to a recipe

Entity Relationship Diagrams

Version 1

Ingredients
id primary key
name string
unit string
created_at datetime
updated_at datetime

Version 2

Recipe has many Ingredients

Ingredients belongs to Recipe

Recipes -|--< Ingredients

Ingredients Recipes
id primary key id primary key
name string name string
unit string description string
recipe_id foreign key created_at datetime
created_at datetime updated_at datetime
updated_at string

Tasks

Developers should run these often!

  • bin/rake routes lists the endpoints available in your API.
  • bin/rake test runs automated tests.
  • bin/rails console opens a REPL that pre-loads the API.
  • bin/rails db opens your database client and loads the correct database.
  • bin/rails server starts the API.

Additional Resources

week_05-day_01-rails-one-to-many's People

Contributors

micfin avatar

Watchers

James Cloos 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.