Skip to content

Instantly share code, notes, and snippets.

@masciugo
Last active August 29, 2015 14:05
Show Gist options
  • Save masciugo/e74c3fad951445617375 to your computer and use it in GitHub Desktop.
Save masciugo/e74c3fad951445617375 to your computer and use it in GitHub Desktop.
setup activerecord and db to test in irb (outside rails box)
require "rubygems"
require 'active_record'
require "sqlite3"
require "factory_girl"
require 'database_cleaner'
SQLite3::Database.new "test.sqlite3"
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "test.sqlite3"
)
conn = ActiveRecord::Base.connection
ActiveRecord::Migration.class_eval do
drop_table(:items) if conn.table_exists? :items
create_table(:items) do |t|
t.column :title, :string
t.column :state, :integer
end
end
class Item < ActiveRecord::Base
enum state: [:on,:off]
after_initialize :defaults
def defaults
self.state ||= :on
end
validates :state, presence: true
end
FactoryGirl.define do
factory :item do
title "Foo"
end
end
i = FactoryGirl.create(:item)
puts i.valid?
puts i.state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment