Created
May 6, 2019 11:04
-
-
Save MittalPatel-BTC/d89085435b2415d9d294d5fec314cd5d to your computer and use it in GitHub Desktop.
Rails 6 added new method of_kind? on ActiveModel::Errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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