-
-
Save jacksonpires/92aaf2267ff731034578268bc4aa758a to your computer and use it in GitHub Desktop.
An example of how to add a custom predicate to Arel
This file contains hidden or 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
module Arel::Predications | |
def has_key(right) | |
Arel::Nodes::HasKey.new(self, quoted_node(right)) | |
end | |
end | |
class Arel::Nodes::HasKey < Arel::Nodes::Binary | |
def operator; :"?" end | |
end | |
class Arel::Visitors::PostgreSQL | |
private | |
def visit_Arel_Nodes_HasKey o, collector | |
infix_value o, collector, " #{Arel::Nodes::HasKey.new(nil, nil).operator} " | |
end | |
end |
This file contains hidden or 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
Person.where(Person.arel_table[:address].has_key('borough')).to_sql | |
# "SELECT \"people\".* FROM \"people\" WHERE (\"people\".\"address\" ? 'borough')" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment