New error in ActiveRecord when activating user from rails c

You are failing validation here, that’s why.

e.g. the password is too short.

You can bubble up the exception doing this:

u.save!

which gives, e.g.:

ActiveRecord::RecordInvalid: Validation failed: Password is too short (minimum is 10 characters)
from /home/robert/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.8.1/lib/active_record/validations.rb:80:in `raise_validation_error'

A quicker way of getting to that would have been to add an exclamation mark after the create:

 u = User.create!(username: "dunderhead", email: "dunderhead@live.com", password: "password")

An ID will not be assigned until you give the object fully valid attributes, either on create or on save after fixing them.

Only when you have an instantiated object can you proceed to run methods on it.

4 Likes