0
votes

I've setup a new app with Devise in Rails 5 and added a custom field "username" to my User model. I added a validation for :username, presence: true and a not null specification in the database migration for the username column.

When I submit my new user form it passes the username validation on the model but is throwing a Postgres database error of "null value in column 'username' violates not-null constraint".

Additionally, when I try to create a new user in the console with User.create!({username: 'Test', email: '[email protected]', password: '12345', password_confirmation: '12345'}) it passes the model validation but throws the same Postgres database error of null value on the username column.

This indicates that the username is getting passed to the model for validation but not getting passed to the database and so Postgres is receiving a null value and throwing a not-null constraint error.

Is there a step I am missing in enabling Devise to accept a custom field on the User model? I've followed all the instructions that I could find from Googling but I seem to have a different problem than other people are experiencing.

1
Just to test things out I removed the not null constraint from the database column and confirmed that a new user can be created but the username value is not getting passed to the database and so is created with a value of nil despite passing the model validation of presence: true. How do I get the value to pass from the model to the database?jfredson

1 Answers

0
votes

Wow, I'm an idiot. I had a special non-db field for an invite code that needed an attr_accessor in the User model to make it work and I added username to that attr_accessor out of habit from the old Rails days. Removing the username attr_accessor from the model all of a sudden made everything work fine again. Doh!