Giter Site home page Giter Site logo

orm-mapping-db-to-ruby-object-lab-v-000's Introduction

ORM Mapping Database Rows to Ruby Objects

Objectives

  1. Write a method that turns database rows into Ruby objects
  2. 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 adminstrators keep track of their students. In this app we have a Student class. Each individual students 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 ourself.

The .find_by_name Method

This is a class method that accepts a name of a student. You will first run a SQL query to run 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 an student instance for each row that comes back from the database.

The .count_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_student_in_grade_10

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

View this lesson on Learn.co

orm-mapping-db-to-ruby-object-lab-v-000's People

Contributors

joshuabamboo avatar victhevenot avatar annjohn avatar jj54321 avatar sophiedebenedetto avatar bacitracin avatar babyshoes avatar

Watchers

 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.