Module: BelongsToHstore::HstoreQueryHelper::ClassMethods

Defined in:
lib/belongs_to_hstore/hstore_query_helper.rb

Instance Method Summary collapse

Instance Method Details

#where_hstore(hstore_attribute, opts) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/belongs_to_hstore/hstore_query_helper.rb', line 6

def where_hstore(hstore_attribute, opts)
  opts.inject(self) do |scope, opt|
    if opt[1].is_a?(Array)
      values = opt[1].map { |val| val.respond_to?(:id) ? val.id.to_s : val.to_s }
      scope.where("#{table_name}.#{hstore_attribute} -> '#{opt[0]}' IN (?)", values)
    else
      if opt[1].nil?
        value = 'NULL'
      else
        value = opt[1].respond_to?(:id) ? opt[1].id : opt[1]
      end

      scope.where("#{table_name}.#{hstore_attribute} @> '#{opt[0]} => #{value}'")
    end
  end
end