Giter Site home page Giter Site logo

rails-many-to-many-lab's Introduction

Modeling Relationships in Rails

Objectives

After this lesson, students will be able to:

  • Build a Rails application that uses a many-to-many relationship

Preparation

Before this lesson, students should already be able to:

  • Create models that inherit from ActiveRecord
  • Use common ActiveRecord associations
  • Explain and generate migrations
  • Describe a relational database

User Stories

Sprint 0

Create a new rails app called movie_almanac. Remember to use a Postgres database and you can choose whether to include tests or not.

Sprint 1

Add a many-to-many relation between actors and movies.

For this app, here's the attributes we're tracking:

  • Actor: first_name, last_name
  • Movie: title, description, year

You can use either the has_and_belongs_to_many or a pair of has_many :through relationships to create this

Basic hint. You should have at least two models and three migrations

Once you've created your models. This seed file should work:

Actor.destroy_all
Movie.destroy_all

morgan = Actor.create first_name: 'Morgan', last_name: 'Freeman'
jim = Actor.create first_name: 'Jim', last_name: 'Carrey'
kevin = Actor.create first_name: 'Kevin', last_name: 'Bacon'

planes = Movie.create title: 'Planes, Trains, and Automobiles', description: 'Comedy', year: 1987
spider = Movie.create title: 'Along came a spider', description: 'Thriller', year: 2001
bruce = Movie.create title: 'Bruce Almighty', description: 'Comedy', year: 2003
cable = Movie.create title: 'Cable Guy', description: 'Drama', year: 1996
apollo_13 = Movie.create title: 'Apollo 13', description: 'Drama', year: 1995

kevin.movies << planes
morgan.movies << spider
morgan.movies << bruce
jim.movies << bruce
jim.movies << cable
kevin.movies << apollo_13

Sprint 2

Create two routes movies and actors. In the movies route, include all of the movies all with the first and last name of any actor who was in the movie. For the actors route include all of the actors and list each movie the actor was in.

Sprint Bonus

  • Implement show for actors and movies
  • Enable a user to use add a new actor to a movie. Enable a user to add a new movie to an actor. Think about how you would use other input types, <selection> cough, cough, to help maintain the data consitence of this action.

Useful Docs

Licensing

All content is licensed under a CC­BY­NC­SA 4.0 license. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

rails-many-to-many-lab's People

Contributors

fishcharlie avatar jpbarela avatar

Watchers

 avatar  avatar  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.