Skip to content

alexmattson/ActiveRecordLite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActiveRecord Lite

Demo:

  1. Clone me
  2. $ ruby demo.rb
  3. Enjoy

Summary:

ActiveRecordLite is an exercise in building my own very stripped down version of ActiveRecord.

The purpose was to understand more deeply how ActiveRecord actually works, particularly how ActiveRecord translates associations and queries into SQL.

Screenshot

Usage:

require_relative 'lib/active_record_lite'

# open database connection
# below command will auto-generate a seeded db/cats.sqlite3
DBConnection.reset

Next, define a model:

class Human < SQLObject
  self.table_name = 'humans'
  my_attr_accessor :id, :fname, :lname, :house_id

  has_many :cats, foreign_key: :owner_id
  belongs_to :house
end

The table name "humans" will be will be overridden since our program defaults to "humen" and we do not want that. To override the default, call self.table_name = "new_name"

By using my_attr_accessor, we allow mass-assignment:

devon = Human.new(fname: 'Devon', lname: 'Watts', house_id: 1)

The foreign_key for has_many :cats is auto-generated to be :human_id. This is wrong in the case of our seed data. For this reason has_many and belongs_to associations accept overrides for :class_name, :foreign_key, and :primary_key:

has_many :cats,
foreign_key: :owner_id,
class_name: 'Cat',
primary_key: :id

Last, there is support for has_one_through:

class Cat < SQLObject
  set_table_name 'cats'
  my_attr_accessor :id, :name, :owner_id

  belongs_to :human, foreign_key: :owner_id
  has_one_through :house, :human, :house
end

and has_many_through:

class House < SQLObject
  my_attr_accessor :id, :address

  has_many :humans
  has_many_through :cats, :humans, :cats
end

Developed by Alex Mattson

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages