I have an SQL query I'm trying to convert to AREL. It starts out:
SELECT COUNT(id) > 0 AS exists...
So far, I have:
Arel::Table.new(:products)[:id].count.gt(0).as(:exists)
but I get:
NoMethodError - undefined method `as' for #<Arel::Nodes::GreaterThan:0x007fc98c4c58d0>
Any ideas?
I'm not sure this is possible: gt
, eq
, etc. are expressions used in the WHERE part of the query. What you're trying to do here is operate in the list of fields that is part of the SELECT, which is handled by Arel's project
method. This is valid:
Arel::Table.new(:products).project(product[:id].count.as(:exists))
But it won't work if you add the condition gt(0)
This isn't fancy but it does what you need:
Arel::Table.new(:products).project(Arel.sql('COUNT(id) > 0').as('exists'))