Giter Club home page Giter Club logo

active-record-review-discussion-questions-yale-web-yss-052719's Introduction

ActiveRecord Review Discussion

ActiveRecord

Before we learned about ActiveRecord, we were able to call on a class method such as Pet.all which would return a collection of all the pet instances. That method would look like this:

  class Pet
    # ...

    def self.all
      ALL
    end

    # ...
  end

When our class inherits from ActiveRecord::Base, we get the all method (and many more methods) for free.

Discuss with your table the steps involved in ActiveRecord's implementation of the class method all. Read the SQL logger output. How is SQL used? What's the return value of this method?

Domain Modeling

With your table discuss how you would model out the relationships between three models: Voter, Vote, and Candidate. On which table do the foreign keys belong?

AR Query Methods

Take a look at the following Voter class:

class Voter < ActiveRecord::Base
  has_many :votes
  has_many :candidates, through: :votes
end

By providing the macros has_many :votes and has_many :candidates, through: :votes, ActiveRecord gives the Voter class the instance methods Voter#votes and Voter#candidates. These methods will fire some SQL, grab some rows from the database, and return the appropriate Ruby instances.

Your task is to run the following commands and read the SQL logger outputs. If you feel very comfortable or passionate about SQL, you can even try and guess what SQL commands will run under the hood when these methods are triggered:

voter = Voter.create

candidate = Candidate.create

voter.votes

voter.candidates

vote = Vote.create(voter_id: voter.id, candidate_id: candidate.id)

vote = voter.votes.create(candidate: candidate)

vote.voter

active-record-review-discussion-questions-yale-web-yss-052719's People

Contributors

telegraham avatar sylwiavargas avatar

Watchers

James Cloos avatar Kaitlin Vignali avatar Mohawk Greene avatar Victoria Thevenot avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar Ben Oren avatar Matt avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar  avatar Vicki Aubin avatar Maxwell Benton 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.