Skip to content

Instantly share code, notes, and snippets.

@jimmynguyc
Last active November 21, 2015 22:07
Show Gist options
  • Save jimmynguyc/864f2a59c6d34d00b253 to your computer and use it in GitHub Desktop.
Save jimmynguyc/864f2a59c6d34d00b253 to your computer and use it in GitHub Desktop.
ActiveRecord without Rails
require 'active_record'
require 'sqlite3'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "teachers.db"
)
# Migration
ActiveRecord::Schema.define do
unless ActiveRecord::Base.connection.table_exists?(:teachers)
create_table :teachers do |table|
table.column :first_name, :string
table.column :last_name, :string
table.column :email, :string
table.column :age, :integer
end
end
end
# Define model
class Teacher < ActiveRecord::Base
validates :first_name, :last_name, :email, presence: true
end
# Seed data
Teacher.create(first_name: 'John', last_name: 'Doe', email:'[email protected]', age: 33)
Teacher.create(first_name: 'Jane', last_name: 'Doe', email:'[email protected]', age: 34)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment