Last active
January 4, 2017 03:02
-
-
Save radar/8828062ffd71cce019d34a67316e07b2 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
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