Last active
November 21, 2015 22:07
-
-
Save jimmynguyc/864f2a59c6d34d00b253 to your computer and use it in GitHub Desktop.
ActiveRecord without Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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