Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created May 21, 2014 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mayfer/b9f4626a44f779062960 to your computer and use it in GitHub Desktop.
Save mayfer/b9f4626a44f779062960 to your computer and use it in GitHub Desktop.
Generic ORM example
require 'pg'
class Orm
@@conn = PG.connect(
dbname: 'dfq35t7uirbums',
port: 5432,
user: 'bmdjwluxchptuq',
host: 'ec2-54-204-41-178.compute-1.amazonaws.com',
password: 'aEH-cKdr2zoXYUAjI8Xjma5eXK'
)
def initialize(*args)
end
def self.all
table = "#{self.name.downcase}s"
puts "table name is #{table}"
instances = []
@@conn.exec("SELECT * FROM #{table}").each do |row|
instance = self.new(*row.values)
instances << instance
end
instances
end
def save
end
def destroy
end
end
class Author < Orm
def initialize(id, last_name, first_name)
@first_name = first_name
@last_name = last_name
@id = id
end
def to_s
"#{@first_name} #{@last_name}"
end
end
authors = Author.all
authors.each do |author|
puts author
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment