Skip to content

Instantly share code, notes, and snippets.

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