Created
February 8, 2019 12:42
-
-
Save annikoff/49f2f4762a7e6c6649c776f36e9454ad 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 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