Created
May 6, 2019 11:06
-
-
Save MittalPatel-BTC/8748b3f98632ad83e172db56f05aa745 to your computer and use it in GitHub Desktop.
Rails 5.2 slice! method
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 5.2 | |
class User < ApplicationRecord | |
validates :email, 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:0x00007fc46700df10 @base=#<User id: nil, name: nil, email: nil, rating: 0, password: nil, created_at: nil, updated_at: nil>, @messages={:email=>["can't be blank"], :password=>["can't be blank"]}, @details={:email=>[{:error=>:blank}], :password=>[{:error=>:blank}]}> | |
> user.errors.slice! | |
=> Traceback (most recent call last): | |
1: from (irb):16 | |
NoMethodError (undefined method 'slice!' for #<ActiveModel::Errors:0x00007fa1f0e46eb8>) | |
Did you mean? slice_when | |
> errors = user.errors.to_h | |
> errors.slice!(:email) | |
=> {:password=>["can't be blank"]} | |
> errors | |
=> {:email=>["can't be blank"]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment