Skip to content

Instantly share code, notes, and snippets.

@jwashke
Created March 24, 2016 17:19
Show Gist options
  • Save jwashke/750d25b817e33af08404 to your computer and use it in GitHub Desktop.
Save jwashke/750d25b817e33af08404 to your computer and use it in GitHub Desktop.

Sequel

SQL: Declarative RUBY: Object oriented

Object Relational Mappers

Written in an object oriented language and wrapped around a relational database The object classes are mapped to the data tables in the database and the object instances are mapped to individual rows in that database

Ruby ORMs

  • Active Record (used a lot)
  • DataMapper (used a bit)
  • Sequel (pretty much not used at all)

Sequel Basics

database = Sequel.sqlite('database.sqlite3')
database.run 'CREATE TABLE people (id integer primary key autoincrement, name varchar(255))"
database.fetch "SELECT * FROM people;"
database.fetch "SELECT * from people;" do |data|
	puts data[:name]
end

Sequel abstracts SQL

database[:items].select

OR

# same as above command        
database.from(:items)      
# select the descriptions from the items table      
database.from(:items).select(:description)      
# select the id and name from the items table     
database.from(:items).select([:id, :name])     
# Using where to scope down      
database.from(:items).where(:name => 'toy')     
database.from(:people).where(:first_name => 'George').where(:phone_number => '111-111-1111')

database.from(:people).where(:id => 1).update(:last_name => "Smith")

database.from(:people).where(:id => 2).delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment