Last active
August 29, 2015 14:05
-
-
Save masciugo/e74c3fad951445617375 to your computer and use it in GitHub Desktop.
setup activerecord and db to test in irb (outside rails box)
This file contains hidden or 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 "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