Last active
September 20, 2020 23:16
-
-
Save gabeodess/c33b192e6d645f37f910 to your computer and use it in GitHub Desktop.
An example of how to add a custom predicate to Arel
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
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 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