Skip to content

barelyknown/enum_to_sym

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enum_to_sym

Ruby on Rails added support for enum columns in ActiveRecord in version 4.1.

Unfortunately, the return value of an enum attribute is a string which seems inconsistent with the rest of the feature and it's not clear if this will get changed.

class Foo < ActiveRecord::Base
  enum status: [:foo, :bar]
end

> foo = Foo.new(status: :bar)
> foo.status  # => "bar" 👎

This gem changes that behavior so that enum attributes return symbols instead. It also adds an is?(name) method as an alternative based on a comment made in this thread.

class Foo < ActiveRecord::Base
  enum status: [:foo, :bar]
end

> foo = Foo.new(status: :bar)
> foo.status  # => :bar 👍
> foo.is?(:bar) # => true

Add this to your gemfile and your enum attributes will return as symbols.

gem 'enum_to_sym'