Giter VIP home page Giter VIP logo

active-record-practice-model-class-methods-lab-seattle-web-career-040119's Introduction

Optimal Queries using Active Record

Learning Goals

  • Use ActiveRecord's AREL library to build optimized queries

Introduction

In programming, a good maxim is this:

Use the best tool for the job

For example, you don't want to use JavaScript to build a computer for flying to the Moon. JavaScript doesn't have very good decimal precision and, at distances as far as the Moon, getting a number off in the hundred-thousandths point after the decimal is the difference between landing on that celestial orb or taking a long trip through nothing, forever.

Databases are AMAZING at linking and summarizing data. Ruby is a nice general-purpose programming language. So when we need to get data from a database, we want to ask the DATABASE to do as much of that work as possible. That's what it's good at. That's what it likes to do. It has sacrificed some capabilities in order to do other capabilities extremely well.

If you use this code:

doctors = Doctor.all
first_six_drs = doctors[0..5]

You will get you six doctors by using RUBY to "section off" six doctors using Ruby's range method ([]). But under the covers we asked the database for all the doctors and then took six of them. Wouldn't it make more sense to ask the database to get us only six Doctors in the first place? That's what the following code does:

Doctor.limit(6).to_a

Functions like limit are provided by the "AREL" engine that's built into ActiveRecord. AREL stands for "A Relational Algebra." If that sounds like some complex, awesome Mathematics and set theory stuff, it is! Fortunately, we don't have to get advanced degrees in mathematics to benefit from this engine. AREL lets us query the database, via ActiveRecord in an object-oriented-looking way but that uses as much of the database's power as possible.

Use ActiveRecord's AREL Library To Build Optimized Queries

In this lab, we've provided the solution (commented out) to the tests. You should step through the tests and "fix" each method to make the test pass.

As you uncomment, be sure to evaluate the implementation we've provided you. Methods like order, where, includes are all part of the AREL engine. You should look up these methods in the AREL documentation, and see how they're working to filter the data retrieved from the database before the result "gets to Ruby-land."

Conclusion

While it's not necessary to memorize all the chainable methods AREL provides ActiveRecord, it's best to know some of the common methods you saw in this lab. If you are working in a Rails environment realizing that AREL can make your queries more efficient can literally speed up your applications 1000x!

Resources

View AREL Lab on Learn.co and start learning to code for free.

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.