Today I Learned

hashrocket A Hashrocket project

ActiveModel Error Details as a Hash

Today I Learned about a new way to list the errors for an invalid ActiveRecord model. Let's say I have a blog post class with title and author attributes, both of which are required:

class BlogPost < ApplicationRecord
  validates :title, presence: true
  validates :author, presence: true
end

Normally I interact with the errors in an invalid model through the nice ActiveModel Errors interface:

pry(main)> blog_post = BlogPost.new
pry(main)> blog_post.valid?
pry(main)> blog_post.errors
# => <ActiveModel::Errors [#<ActiveModel::Error attribute=title, type=blank, options={}>, #<ActiveModel::Error attribute=author, type=blank, options={}>]>

pry(main)> blog_post.errors.where(:title)
# => [#<ActiveModel::Error attribute=title, type=blank, options={}>]

But you can also return a hash of attributes containing arrays of their error details and use your favorite hash methods:

pry(main)> blog_post.errors.details
# => {:title=>[{:error=>:blank}], :author=>[{:error=>:blank}]}

pry(main)> blog.errors.details.dig(:title, 0, :error)
# => :blank
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.