Every field will save to the database except one!

Hi folks.

I'm a rank amateur with both Ruby and Rails but have nonetheless found myself working on a codebase. I've been able to muddle my way through so far, but have run into a problem that's been breaking my head for hours and I'm hoping someone might have some suggestions.

(The codebase was written in Ruby 1.9.3 and has been moved across to 2.4.1. The code worked fine originally so there's presumably some version difference behind the issue.)

I have a string called 'email' that contains an email address. A new User object (called 'contact') is created as follows...

contact = User.new(:email => email)

Other fields are added to 'contact', including a duplicate of the email address in a 'username' field...

contact.password = passwordString
contact.username = email

The object is then saved...

if contact.save
contactID = contact.indexid
end

It saves successfully and a new row is created in the User table, *but the email field is left blank.*

Checking the properties of 'contact' both before and after the save shows that contact.email contains the correct data, it's just that no matter what I do I can't get it to save in the database. All other fields seem to be able to be manipulated and saved fine, but email refuses to be saved.

The User model has attr_accessor set for the email field, and changing contact.email in the code seems to work fine, it just will not save the contents.

I'm hoping there is something really simple that I'm missing, but any suggestions will be gratefully accepted!

1 thought on “Every field will save to the database except one!”

  1. If it is a Ruby 1.9.3 app, is it safe to assume that it is also Rails 3.x?

    Try putting `attr_accessible :email` in the User model to see if that will allow the email to save.

    Also, in the database, is there a field for the email?

    Reply

Leave a Comment