Skip to content

find_first fails with "undefined method first" #1

@olance

Description

@olance

I'm trying to use cequel-devise with... Devise ^^

I have a User model:

# -*- encoding : utf-8 -*-
class User
  include Cequel::Record

  devise :database_authenticatable, :registerable

  key :email, :varchar
  column :username, :varchar
  column :encrypted_password, :varchar
  timestamps
end

Partitioning with the email seemed to me the best way to integrate with Devise, which will be able to efficiently look for a user by email.

Signing up goes well, but when I try to log in, I end up with the following error:

NoMethodError: undefined method `first' for #<User email: "[email protected]">

The error is located here: orm_adapter-cequel (1.0.1) lib/cequel/record/orm_adapter.rb:61:in 'find_first'.

From what I've gathered, find_first tries to build a scope using the given options to query the DB and to get the first record by calling first on that scope.

scope is assigned the following values:

  • scope = klass.all #=> User
  • scope = apply_secondary_index_scope(scope, conditions) || apply_primary_key_scope(scope, conditions) #=> User['[email protected]'] => #<User:0x007f8bc4d0e6c8>

conditions only contains an email key with value [email protected]. In the second step, this resolves to applying this key as a "primary key scope", which ends up restricting the scope to a single User instance.

I guess it's because of this behavior of #[] (from cequel/cequel:lib/cequel/record/record_set.rb):

    # If {#[]} is used successively to specify all of the columns of a primary
    # key, the result will be a single {Record} or a {LazyRecordCollection},
    # depending on whether multiple values were specified for one of the key
    # columns. In either case, the record instances will be unloaded.

Because of my relational-like table, where a partition key (email) always maps to a single User instance, #[] will always return a single Record instance.
I guess find_first should call first on the returned scope only if scope is an instance of RecordSet?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions