Today I Learned

hashrocket A Hashrocket project

Easily Update ActiveRecord Enum Values

Today I learned about some nice helper methods for ActiveRecord Enums. You might already know the ? methods to check the value of the enum. There are also ! methods to succinctly set the value of the enum too!

class Post < ActiveRecord::Base
  enum status: {
    draft: 0,
    published: 1
  }
end

post = Post.new
post.published? # => false
post.published! # updates the status to `published`
post.published? # => true
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.