Created
August 30, 2015 00:21
-
-
Save samhains/52a4a45af522d938befa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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