1

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?

wuliwong
  • 4,238
  • 9
  • 41
  • 69
  • not sure if that applies to dm as well but bang the save method and see if it throws an error: @user.save! – three Mar 17 '12 at 11:42
  • Nope, datamapper actually follows the ruby convention of `@user.save!` being the dangerous method. Try `@user.save_or_raise`. – Reactormonk Mar 17 '12 at 14:56
  • It seems in datamapper you set User.raise_on_save_failure = true. And I do indeed get this error #. Not sure where to go from there, but it confirms that I'm trying to save it at least. :) – wuliwong Mar 17 '12 at 19:30

2 Answers2

2

According DataMapper's docs default length of string is 50 chars. Just use correct value in property declaration:

    property :validation_code, String, :length => 500
2

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....

wuliwong
  • 4,238
  • 9
  • 41
  • 69