Skip to content

Instantly share code, notes, and snippets.

@MittalPatel-BTC
Created May 6, 2019 11:04
Show Gist options
  • Save MittalPatel-BTC/d89085435b2415d9d294d5fec314cd5d to your computer and use it in GitHub Desktop.
Save MittalPatel-BTC/d89085435b2415d9d294d5fec314cd5d to your computer and use it in GitHub Desktop.
Rails 6 added new method of_kind? on ActiveModel::Errors
# Rails 6
class User < ApplicationRecord
validates :name, presence: true
end
> user = User.new
=> #<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil>
> user.valid?
=> false
> user.errors
=> #<ActiveModel::Errors:0x00007fc462a1d140 @base=#<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil>, @messages={:name=>["can't be blank"]}, @details={:name=>[{:error=>:blank}]}>
> user.errors.of_kind?(:name)
=> false
> user.errors.of_kind?(:name, :blank)
=> true
> user.errors.of_kind?(:name, "can't be blank")
=> true
> user.errors.of_kind?(:name, "is blank")
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment