Search code examples
sinatrabooleanmodelsdatamapperupdating

Can't save updates to values in datamapper model


I have this model

class User
  include DataMapper::Resource
  include BCrypt

  property :id, Serial
  property :email, String, :required => true
  property :password_hash, Text, :required => false
  property :user_name, String, :required => true
  property :birthdate, Date
  property :city, String
  property :state, String
  property :zip, String
  property :bio, Text
  property :validated, Boolean, :default => false
  property :validation_code, String
  property :created_at, DateTime
  property :updated_at, DateTime
end

I have this route

get '/validate/:code/:user_id/?' do
  @user = User.get params[:user_id]

  if @user.validation_code == params[:code]
    @user.validated = true
    @user.save
  end

  erb :validate
end

@user.validated is being set to true but it is not being saved. I'm assuming this is something very simple, but I can't seem to figure it out. Anyone know why the assignment of true to @user.validated is not being saved?

Thanks.

------Update----------

When I tell datamapper to raise on failure

User.raise_on_save_failure = true

I receive this error

#<DataMapper::SaveFailureError: User#save returned false, User was not saved>

I know it isn't much, but I know that I'm trying to save and datamapper doesn't like what I'm doing. I know that datamapper doesn't like "dirty" records, but I don't see that being the case here, right?


Solution

  • There was no way you guys could have figured this out from the information I provided. :) The problem was the string I was generating for :validation_code was actually too long. Once I chopped the validation code down to the right size (less than 50 characters I think?) everything works properly.

    This is yet another lesson for me to not cut corners and add things one at a time. sigh....