Module: Cequel::Record::Finders

Defined in:
lib/cequel/record/finders.rb

Overview

Cequel provides finder methods to construct scopes for looking up records by primary key or secondary indexed columns.

Examples:

Example model class

class Post
  key :blog_subdomain, :text
  key :id, :timeuuid, auto: true
  column :title, :text
  column :body, :text
  column :author_id, :uuid, index: true # this column has a secondary
                                        # index
end

Using some but not all primary key columns

# return an Array of all posts with given subdomain (greedy load)
Post.find_all_by_blog_subdomain(subdomain)

# return a {RecordSet} of all posts with the given subdomain (lazy
# load)
Post.with_subdomain(subdomain)

Using all primary key columns

# return the first post with the given subdomain and id, or nil if none
Post.find_by_blog_subdomain_and_id(subdomain, id)

# return a record set to the post with the given subdomain and id
# (one element array if exists, empty array otherwise)
Post.with_blog_subdomain_and_id(subdomain, id)

Chaining

# return the first post with the given subdomain and id, or nil if none
# Note that find_by_id can only be called on a scope that already has a
# filter value for blog_subdomain
Post.with_blog_subdomain(subdomain).find_by_id(id)

Using a secondary index

# return the first record with the author_id
Post.find_by_author_id(id)

# return an Array of all records with the author_id
Post.find_all_by_author_id(id)

# return a RecordSet scoped to the author_id
Post.with_author_id(id)

Since:

  • 1.2.0