Skip to content

Instantly share code, notes, and snippets.

@samhains
Created August 30, 2015 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhains/52a4a45af522d938befa to your computer and use it in GitHub Desktop.
Save samhains/52a4a45af522d938befa to your computer and use it in GitHub Desktop.
# Performs an attribute search on the serialized object
def self.where_object(args = {})
fail ArgumentError, 'expected to receive a Hash' unless args.is_a?(Hash)
if columns_hash['object'].type == :jsonb
where_conditions = "object @> '#{args.to_json}'::jsonb"
elsif columns_hash['object'].type == :json
where_conditions = args.map do |field, value|
"object->>'#{field}' = '#{value}'"
end
where_conditions = where_conditions.join(" AND ")
end
where(where_conditions).order('created_at DESC')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment