Skip to content

Instantly share code, notes, and snippets.

@annikoff
Created February 8, 2019 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save annikoff/49f2f4762a7e6c6649c776f36e9454ad to your computer and use it in GitHub Desktop.
Save annikoff/49f2f4762a7e6c6649c776f36e9454ad to your computer and use it in GitHub Desktop.
An example of how to add a custom predicate to Arel
module Arel::Predications
def any(right)
Arel::Nodes::Any.new(self, quoted_node(right))
end
end
class Arel::Nodes::Any < Arel::Nodes::Binary
def operator
:'ANY'
end
end
class Arel::Visitors::PostgreSQL
private
def visit_Arel_Nodes_Any(o, collector)
collector = visit o.right, collector
collector << " = #{Arel::Nodes::Any.new(nil, nil).operator} ("
visit(o.left, collector) << ")"
end
end
puts User.where(User.arel_table[:roles].any('superadmin')).to_sql
SELECT "users".* FROM "users" WHERE ('superadmin' = ANY ("users"."roles"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment