Git Product home page Git Product logo

dynamic-orm-lab-v-000's Introduction

#Dynamic ORM Lab

Objectives

  1. Construct an ORM superclass that can be used for multiple classes
  2. Inherit the methods from the ORM class to the Student class

Dynamic ORMs & Inheritance

By this point, we've learned the value of a dynamic ORM -- a way to write an ORM that is almost entirely abstract. The key takeaway is that this ORM is not specific to any one class. The code will live in one class (the superclass) and be inherited to any class (child class) we wish to have access to the ORM methods.

Now that we've had some practice with this, let's build out a dynamic ORM that can be inherited to any class.

Let's get started

The Super Class

In the lib directory, you'll see the interactive_record.rb file. This file is where almost all of your ORM code will live. Once you set this up, you will share the methods in this class with the child class.

The Child Class

Your Student class lives in lib/student.rb. This class will inherit from InteractiveRecord. Your methods won't be written in this class. All of the methods defined in InteractiveRecord will be available to Student once you set up the inheritance.

Note: The only code the Student class needs to contain is the code to create the attr_accessors specific to itself. But even that code uses a method, #column_names, inherited from the super class.

This is a test-driven lab, so run the specs and get them to pass. (Note: You might need to install some dependencies to get the tests running โ€” that's okay! As Jedi Master Obi-Wan Kenobi would say, "Use the Gemfile, Luke!")

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

dynamic-orm-lab-v-000's People

Contributors

annjohn avatar curiositypaths avatar dakotalmartinez avatar drakeltheryuujin avatar gj avatar joshuabamboo avatar maxwellbenton avatar pletcher avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dynamic-orm-lab-v-000's Issues

Specs outdated with SQLite3 gem version bump to 1.4.0

During this lab, I was struggling getting the #save method to pass the specs. I was having trouble understanding why I was getting the following error when running learn --fail-fast

Failures:

  1) Student has instance methods to insert data into db #save saves the student to the db
     Failure/Error: expect(DB[:conn].execute("SELECT * FROM students WHERE name= 'Sam'")).to eq([{"id"=>1, "name"=>"Sam", "grade"=>11, 0=>1, 1=>"Sam", 2=>11}])

       expected: [{"id"=>1, "name"=>"Sam", "grade"=>11, 0=>1, 1=>"Sam", 2=>11}]
            got: [{"grade"=>11, "id"=>1, "name"=>"Sam"}]

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -[{"id"=>1, "name"=>"Sam", "grade"=>11, 0=>1, 1=>"Sam", 2=>11}]
       +[{"grade"=>11, "id"=>1, "name"=>"Sam"}]

It didn't make sense to me that the test would be expecting those last three columns (e.g. 0=>1, 1=>"Sam", 2=>11), since those index values aren't actual columns in the SQL table. After spending some time with technical coaches trying to figure out the issue, we discovered we needed to point the sqlite3 gem version to 1.3.13 in the Gemfile. This allowed the tests to pass, but it seems that the expectations of the tests should be adjusted as opposed to specifying the version.

According to https://stackoverflow.com/questions/27065389/ruby-sqlite3-results-as-hash-returns-duplicate-data, the reason the hash and the array associated with these records are being returned is due to setting results_as_hash = true. With the sqlite3 version bump to 1.4.0 on 2/4/19 it seems that's no longer an issue.

Error in Spec File

When the test suite is run, the following error occurs before each test output:
/home/ubuntu/workspace/orms-and-activerecord/orms/dynamic-orm-lab-v-000/spec/student_spec.rb:5: warning: already initialized constant DB
/home/ubuntu/workspace/orms-and-activerecord/orms/dynamic-orm-lab-v-000/spec/student_spec.rb:5: warning: previous definition of DB was here

Removing the redundant definition of the DB constant in the before block of the student_spec.rb file solves this problem. I've opened a pull request containing a commit that solves the issue.

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.