Joe Morano Joe Morano - 1 year ago 63
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

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}


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
to get back the errors that were preventing the record from being saved.

Answer Source

Try this:

user = "test")

user.errors  # This should contain the errors that prevented your object from being saved.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download