Module: Surus::Hstore::Scope

Defined in:
lib/surus/hstore/scope.rb

Instance Method Summary collapse

Instance Method Details

#hstore_has_all_keys(column, *keys) ⇒ Object

Adds a where condition that requires column to contain all keys.

Example:

User.hstore_has_all_keys(:properties, "favorite_color", "favorite_song")
User.hstore_has_all_keys(:properties, ["favorite_color", "favorite_song"])


25
26
27
# File 'lib/surus/hstore/scope.rb', line 25

def hstore_has_all_keys(column, *keys)
  where("#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)} ?& ARRAY[:keys]", keys: keys.flatten)
end

#hstore_has_any_keys(column, *keys) ⇒ Object

Adds a where condition that requires column to contain any keys.

Example:

User.hstore_has_any_keys(:properties, "favorite_color", "favorite_song")
User.hstore_has_any_keys(:properties, ["favorite_color", "favorite_song"])


34
35
36
# File 'lib/surus/hstore/scope.rb', line 34

def hstore_has_any_keys(column, *keys)
  where("#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)} ?| ARRAY[:keys]", keys: keys.flatten)
end

#hstore_has_key(column, key) ⇒ Object

Adds a where condition that requires column to contain key

Example:

User.hstore_has_key(:properties, "favorite_color")


16
17
18
# File 'lib/surus/hstore/scope.rb', line 16

def hstore_has_key(column, key)
  where("#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)} ? :key", key: key)
end

#hstore_has_pairs(column, hash) ⇒ Object

Adds a where condition that requires column to contain hash

Example:

User.hstore_has_pairs(:properties, "favorite_color" => "green")


8
9
10
# File 'lib/surus/hstore/scope.rb', line 8

def hstore_has_pairs(column, hash)
  where("#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)} @> ?", Serializer.new.dump(hash))
end