Skip to content

Instantly share code, notes, and snippets.

@radar
Last active January 4, 2017 03:02
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 radar/8828062ffd71cce019d34a67316e07b2 to your computer and use it in GitHub Desktop.
Save radar/8828062ffd71cce019d34a67316e07b2 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
def self.my_where(key, value)
# Normally with 2 arguments, you can easily do:
where(key: value)
end
def self.where_preference(conditions)
# But with a map and several k-v pairs, you'd have to do something like
conditions.inject(self) do |scope, (k, v)|
scope.where(k => v)
end
# But what is `subject` here so that this can be chainable with other Arel queries?
# The reason I ned this is for an hstore query that looks like:
# where("preferences @> hstore(:key,:value)", key: k, value: v)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment