Skip to content

Instantly share code, notes, and snippets.

@jdickey
Created April 26, 2012 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdickey/2499810 to your computer and use it in GitHub Desktop.
Save jdickey/2499810 to your computer and use it in GitHub Desktop.
Simplest possible use of Enumerize. Trying to track down my bug in Gist 2497174.
gem 'rails', '3.2.3' # change as required
require 'active_record'
require 'enumerize'
require 'awesome_print'
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)
ActiveRecord::Schema.define do
create_table :users, :force => true do |t|
t.string :name
end
create_table :articles, :force => true do |t|
t.integer :author_id
t.string :name
t.string :status
end
end
class User < ActiveRecord::Base
attr_accessible :name
has_many :articles
end
class Article < ActiveRecord::Base
include Enumerize
# attr_accessor :status
# validates_presence_of :name, :status
belongs_to :author, :class_name => 'User'
# ##### KEY LINE BELOW ##### #
enumerize :status, :in => [:new, :draft, :private, :published] #, :default => :new
# ##### KEY LINE ABOVE ##### #
end
author = User.create! :name => 'The Author'
puts "Author valid? " + author.valid?.to_s
article = Article.new :name => 'An Article', :status => :new
article.author = author
article.status = :published
puts "Article valid? " + article.valid?.to_s
awesome_print article
awesome_print article.status.published?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment