Git Product home page Git Product logo

orm-mapping-db-to-ruby-object-lab-onl01-seng-pt-081720's Introduction

ORM Mapping Database Rows to Ruby Objects

Objectives

  • Write a method that turns database rows into Ruby objects
  • Write methods that allow you to search a database

Overview

We are going to continue building out the Student class of our app. We are in the middle of building an app to help a public school's administrators keep track of their students. In this app, we have a Student class. Each individual student has two attributes, a name and a grade, i.e. 9th, 10th, 11th, etc.

Our administrators need to save the student objects that this class produces to a database. We've set up the database for you in the config/environment.rb file.

Let's take a look:

require 'sqlite3'
require_relative '../lib/student'

DB = {:conn => SQLite3::Database.new("db/students.db")}

Your connection to the database can be referred to, throughout your program, like this: DB[:conn].

So far we built the ability to create the students table in the database (Student.create_table), drop that table (Student.drop_table), and #save a student to the database.

Now, we will need to create a method that takes a row from the database and turns it back into a Student object. We will call this .new_from_db.

Next, we want to build a couple of methods to get information from the database. We will call these .find_by_name and .all.

Let's get started!

Instructions

The .new_from_db Method

This is a class method that accepts a row from the database as an argument. It then creates a new student object based on the information in the row. Remember, our database doesn't store Ruby objects, so we have to manually convert it ourselves.

The .find_by_name Method

This is a class method that accepts the name of a student. You will first run a SQL query to get the result from the database where the student's name matches the name passed into the argument.

Next, you will take the result and create a new student instance using the .new_from_db method you just created.

The .all Method

This is a class method that is very similar to the .find_by_name method. You will not need an argument since we are returning everything in the database. Run the SQL to return everything (*) from a table.

Again, you will use the .new_from_db method to create a student instance for each row that comes back from the database.

The .all_students_in_grade_9 Method

This is a class method that does not need an argument. This method should return an array of all the students in grade 9.

The .students_below_12th_grade Method

This is a class method that does not need an argument. This method should return an array of all the students below 12th grade.

The .first_X_students_in_grade_10 Method

This is a class method that takes in an argument of the number of students from grade 10 to select. This method should return an array of exactly X number of students.

The .first_student_in_grade_10 Method

This is a class method that does not need an argument. This should return the first student that is in grade 10.

The .all_students_in_grade_X Method

This is a class method that takes in an argument of a grade for which to retrieve the roster. This method should return an array of all students for grade X.

View ORM Mapping Database Rows to Ruby Objects on Learn.co and start learning to code for free.

orm-mapping-db-to-ruby-object-lab-onl01-seng-pt-081720's People

Contributors

joshuabamboo avatar maxwellbenton avatar annjohn avatar antoinfive avatar drakeltheryuujin avatar gj avatar victhevenot avatar bhollan avatar curiositypaths avatar babyshoes avatar bacitracin avatar sophiedebenedetto avatar peterbell avatar pianistps avatar justinnazari avatar azemoh avatar pletcher avatar

Watchers

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