Giter VIP home page Giter VIP logo

05_activerecord_associations_one_to_many's Introduction

ActiveRecord Associations - One to Many

Learning Goals:

  • [] Recognize the connection between the primary key - foreign key relationship and ActiveRecord associations
  • [] Implement a one to many association between two models
  • [] Implement a one to one association between two models
  • [] Read and understand documentation on ActiveRecord association methods

Important Resources for Today

Topics

What is the difference between a primary key and a foreign key?

...

If we have a one to many relationship between two tables, which table gets the foreign key?

...

Association methods are which kind of methods–instance methods or class methods?

...

Help me build this out

  • Create Walk model and associated walks table
  • open up ./bin/console and do create a couple of dogs.

Task 1 - belongs_to

  1. Create an issues table with the following columns:
  • title (string)
  • description (string)
  • resolved (boolean)
  • started_at (datetime)
  • computer (belongs_to)
  1. In lib/issue.rb, create an Issue class that inherits from ActiveRecord::Base

  2. Use an ActiveRecord association macro to create a relationship between an issue and a computer. Which type of relationship should you add here?

  3. Enter ./bin/console and create a couple of issues for the first_computer. Use Sqlite Explorer to check your database to ensure the issues are created.

Discussion Questions

How did you create the issues table?

...

How would you describe the relationship between an issue and a computer?

...

When you use the association method to access the related computer, what does the SQL query look like?

...

Help me build this out

  • Finish adding the one-to-many relationship between Dog <== Walk

Task 2 - has_many

  1. Update the Computer class to add an ActiveRecord association method that relates the Computer model to the Issue model.

  2. Open up ./bin/console and use it to create an issue for a computer and then access that computer and view all of its issues. Confirm that you're able to call a method on either the issue or the computer and see the other object in the return value.

Discussion Questions

What are the database requirements for a has_many relationship?

...

What about the belongs_to relationship?

...

What other examples of one-to-many relationships can you think of?

...

05_activerecord_associations_one_to_many's People

Contributors

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