Rails, Cequel - Don't know how to cast to a UUID

361 Views Asked by At

I'm following this tutorial (https://medium.com/@olance/rails-4-user-authentication-with-cequel-and-devise-86e1c9ccbcd0), but I'm having problems with the login. When I receive the following error:


ArgumentError in HomeController#index

Don't know how to cast {"n"=>242893962554067921677178635377202198460} to a UUID

Extracted source (around line #423):

      Cql::Uuid.new(value)
    else
      fail ArgumentError,
           "Don't know how to cast #{value.inspect} to a UUID" # Line 423
    end
  end

The model used by devise is:

class User
  include Cequel::Record

  devise :database_authenticatable, :registerable

  key :id, :uuid, auto => true
  column :username, :varchar, :index => true
  column :name, :varchar
  column :lastname, :varchar
  column :email, :varchar, :index => true
  column :encrypted_password, :varchar
  timestamps
end

If I change the key to a varchar it works, but I wan't to use a UUID, so that the user can change the email or its username.

How can it be corrected?

1

There are 1 best solutions below

1
On

If you look in source of method uuid you will see here:

def uuid(value = nil)
  if value.nil?
    timeuuid_generator.now
  elsif value.is_a?(Time)
    timeuuid_generator.at(value)
  elsif value.is_a?(DateTime)
    timeuuid_generator.at(Time.at(value.to_f))
  else
    Type::Timeuuid.instance.cast(value)
  end
end

It means that at it suppose values like nil, Time, DateTime. In none of them it will use method cast of Type::Timeuuid which is located somewhere here

Any of cast method definitions seems to be not awaiting to receivce a hash

As a workaround you could try something like Cql::Uuid.new(value['n']) But you should check out why you get a hash there, and what should it really be