[RUBY] save! method

While creating the flea market apps, I couldn't figure out why the data wasn't saved even though I was able to get the key. When I asked the mentor, he told me how to find the cause, so make a note.

What is the save / save! method?

A method for saving data.

save method

The save method returns true if the save is successful and false if the save fails (validation fails).

save! method

The save! method returns true if the save is successful, but an exception is thrown if the save fails (validation fails). Therefore, it is necessary to perform the processing in the rescue clause when it cannot be saved.

■ In my case, when I used binding.pry, I was able to get the key, so I used@user.save! To find the cause. ↓ Description when it could not be saved

[8] pry(#<Users::RegistrationsController>)> @user.save!
   (0.2ms)  BEGIN
  ↳ (pry):12
  User Exists (13.4ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = BINARY '' LIMIT 1
  ↳ (pry):12
   (1.1ms)  ROLLBACK
  ↳ (pry):12
ActiveRecord::RecordInvalid: Validation failed: Email can't be blank, Email is invalid, Password can't be blank, Nickname can't be blank, Encrypted password can't be blank, Encrypted password is too short (minimum is 7 characters), Encrypted password is invalid, Familyname kanji can't be blank, Familyname kanji is invalid, Firstname kanji can't be blank, Firstname kanji is invalid, Familyname kana can't be blank, Familyname kana is invalid, Firstname kana can't be blank, Firstname kana is invalid, Birthday can't be blank
from /Users/httr_htm/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/validations.rb:80:in `raise_validation_error'

The cause is written after ʻActiveRecord :: RecordInvalid`.

As a result, the description of the regular expression was the cause. I had set the email in the format of ʻ[email protected], but I was stuck in validation because I entered ʻaa @ aa. If I had set the error display, I might have been able to solve it sooner.

I was introduced to a site that checks regular expressions called Rubular. I want to utilize it in the future.

reference

Active engineers explain how to use Rails' save! Method [for beginners]

Recommended Posts

save! method
Method
Java method
to_i method
getRequestDispatcher () method
merge method
include method
Abstract method
List method
puts method
Java method
Class method
getParameter method
[Java] method
private method
rails method
[Java] method
Ruby to_s method
Screen transition method
Binary search binary search method
Method to search
Factory Method Pattern
Exception switching method
RxSwift method chain
Rails delegate method
scan method problem
Java8 method reference
[Ruby] slice method
[Ruby] Method memorandum
About the method
[Java] forEach method
[Practice] Map method
[Servlet] Basic method
variable and method
Binary search method
Template Method pattern
Template Method Pattern
0-filled (non-zero) method
JavaSilver11 study method
keycloak test-skip method
Factory Method pattern
[Ruby] initialize method
Ruby build method
undefined method `** _ path'error
java8 method reference
[Rails] Difference between create method and new + save method
[Java] Random method
Ruby accessor method
[Java] split method
ruby map method