Class: ActiveRecord::QueryMethods::KeyStoreChain

Inherits:
StoreChain
  • Object
show all
Defined in:
lib/pgrel/active_record/store_chain.rb

Overview

Base class for key-value types of stores (hstore, jsonb)

Direct Known Subclasses

HstoreChain, JsonbChain

Instance Attribute Summary

Attributes inherited from StoreChain

#quoted_store_name, #store_name

Instance Method Summary collapse

Methods inherited from StoreChain

#contained, #contains, #initialize, #not, #where

Constructor Details

This class inherits a constructor from ActiveRecord::QueryMethods::StoreChain

Instance Method Details

#any(*keys) ⇒ Object

Any of the keys existence

Example

Model.create!(name: 'first', store: {a: 1, b: 2})
Model.create!(name: 'second', store: {b: 1, c: 3})

Model.store(:store).keys('a','b').count #=> 2


190
191
192
193
194
195
# File 'lib/pgrel/active_record/store_chain.rb', line 190

def any(*keys)
  update_scope(
    "#{quoted_store_name} ?| ARRAY[:keys]",
    keys: keys.flatten.map(&:to_s)
  )
end

#key(key) ⇒ Object

Single key existence

Example

Model.create!(name: 'first', store: {a: 1})
Model.create!(name: 'second', store: {b: 1})

# Get all records which have key 'a' in store 'store'
Model.store(:store).key('a').all #=> [Model(name: 'first', ...)]


165
166
167
# File 'lib/pgrel/active_record/store_chain.rb', line 165

def key(key)
  update_scope "#{quoted_store_name} ? :key", key: key.to_s
end

#keys(*keys) ⇒ Object

Several keys existence

Example

Model.create!(name: 'first', store: {a: 1, b: 2})
Model.create!(name: 'second', store: {b: 1, c: 3})

Model.store(:store).keys('a','b').all #=> [Model(name: 'first', ...)]


176
177
178
179
180
181
# File 'lib/pgrel/active_record/store_chain.rb', line 176

def keys(*keys)
  update_scope(
    "#{quoted_store_name} ?& ARRAY[:keys]",
    keys: keys.flatten.map(&:to_s)
  )
end