Module: Cequel::Record::SecondaryIndexes
- Defined in:
- lib/cequel/record/secondary_indexes.rb
Overview
Data columns may be given secondary indexes. This allows you to query the table for records where the indexed column contains a single value. Secondary indexes are not nearly as flexible as primary keys: you cannot query for multiple values or for ranges of values. You also cannot combine a secondary index restriction with a primary key restriction in the same query, nor can you combine more than one secondary index restrictions in the same query.
If a column is given a secondary index, magic finder methods ‘find_by_*`, `find_all_by_*`, and `with_*` are added to the class singleton. See below for an example.
Secondary indexes are fairly expensive for Cassandra and should only be defined where needed.
Instance Method Summary collapse
Instance Method Details
#column(name, type, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cequel/record/secondary_indexes.rb', line 46 def column(name, type, = {}) super name = name.to_sym if [:index] instance_eval <<-RUBY, __FILE__, __LINE__+1 def with_#{name}(value) all.where(#{name.inspect}, value) end def find_by_#{name}(value) with_#{name}(value).first end def find_all_by_#{name}(value) with_#{name}(value).to_a end RUBY end end |