Joe Morano Joe Morano - 4 days ago 5
Ruby Question

Record creation being rolled back without explanation

I have a model "User" that up until now didn't have any issues. I added some validations and noticed that I no longer could add any new Users - the record would be rolled back. So I removed the validations, but my records were still being rolled back. So I eliminated literally all the code from my model file so all it contains is this:

class User < ActiveRecord::Base
end


but I'm still getting the same error.

In my rails console:

> User.create(name: "test")
(0.6ms) BEGIN
(2.3ms) ROLLBACK
#=> #<User id: nil, name: "test", (et cetera)>


I don't even know how to start figuring out what's wrong. How can I even debug this? All my other models work normally.

This is what I added before this started:

blacklist = ['home'].freeze
validates :name, exclusion: {in: blacklist}


SOLVED:

I integrated Devise with this model, so there were some validations in place that weren't in my Devise.rb file. I had to run
@user.errors
to get back the errors that were preventing the record from being saved.

Answer

Try this:

user = User.new(name: "test")
user.save

user.errors  # This should contain the errors that prevented your object from being saved.
Comments