How do I name a calculated boolean column with AREL?

elijah

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?

Braulio Carreno

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'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I create an automated calculated column?

From Dev

How do I use pandas to add a calculated column in a pivot table?

From Dev

How do I select from a column that might be boolean or the string '1'?

From Dev

How do I fill an R Matrix with values which are calculated from row and column names?

From Dev

How Do I Store A Calculated Value In Rails

From Dev

How to refer a table column as clause condition with Arel?

From Dev

How do I make sql timestamp as column name?

From Dev

How do I alias a column name in Propel query for Oracle?

From Dev

How do I get column name of table from specific database?

From Dev

PDO: how do I fetch a single record by column name

From Dev

How do I rename a blank column name in pandas df?

From Dev

how do i fetch derived/calculated column from database view or Procedure in Spring Boot using JPA/Hibernate and use it along with predefined columns?

From Dev

How do I address Arel incompatibility with Hartl Rails Tutorial (Ch. 2)?

From Dev

How can I update a column with PL\SQL by using a calculated value

From Dev

Ruby Rails - add a boolean checkbox column into existing db and how do i work with it

From Dev

How do I put a constraint on a table to ensure only one boolean column across a subset of tables is true?

From Dev

How do I turn arguments 'integer<120 boolean boolean boolean boolean' into the most compact package possible

From Dev

How do I get text from a calculated String to a TextView field?

From Dev

How do I easily create a calculated field in a Django model?

From Dev

How do I assign a value calculated at runtime in a function to a form control?

From Dev

How do I assign a value calculated at runtime in a function to a form control?

From Dev

How do I modify a collection after being calculated?

From Dev

How do I concatenate a calculated result to some existing text in Twig?

From Dev

How do I "default" a boolean property in JavaScript?

From Java

How do I convert a boolean to an integer in Rust?

From Dev

How do I toggle a boolean array in Python?

From Dev

How do I check a value in a boolean List?

From Dev

How do I shorten this boolean expression?

From Dev

How do I "default" a boolean property in JavaScript?

Related Related

  1. 1

    How do I create an automated calculated column?

  2. 2

    How do I use pandas to add a calculated column in a pivot table?

  3. 3

    How do I select from a column that might be boolean or the string '1'?

  4. 4

    How do I fill an R Matrix with values which are calculated from row and column names?

  5. 5

    How Do I Store A Calculated Value In Rails

  6. 6

    How to refer a table column as clause condition with Arel?

  7. 7

    How do I make sql timestamp as column name?

  8. 8

    How do I alias a column name in Propel query for Oracle?

  9. 9

    How do I get column name of table from specific database?

  10. 10

    PDO: how do I fetch a single record by column name

  11. 11

    How do I rename a blank column name in pandas df?

  12. 12

    how do i fetch derived/calculated column from database view or Procedure in Spring Boot using JPA/Hibernate and use it along with predefined columns?

  13. 13

    How do I address Arel incompatibility with Hartl Rails Tutorial (Ch. 2)?

  14. 14

    How can I update a column with PL\SQL by using a calculated value

  15. 15

    Ruby Rails - add a boolean checkbox column into existing db and how do i work with it

  16. 16

    How do I put a constraint on a table to ensure only one boolean column across a subset of tables is true?

  17. 17

    How do I turn arguments 'integer<120 boolean boolean boolean boolean' into the most compact package possible

  18. 18

    How do I get text from a calculated String to a TextView field?

  19. 19

    How do I easily create a calculated field in a Django model?

  20. 20

    How do I assign a value calculated at runtime in a function to a form control?

  21. 21

    How do I assign a value calculated at runtime in a function to a form control?

  22. 22

    How do I modify a collection after being calculated?

  23. 23

    How do I concatenate a calculated result to some existing text in Twig?

  24. 24

    How do I "default" a boolean property in JavaScript?

  25. 25

    How do I convert a boolean to an integer in Rust?

  26. 26

    How do I toggle a boolean array in Python?

  27. 27

    How do I check a value in a boolean List?

  28. 28

    How do I shorten this boolean expression?

  29. 29

    How do I "default" a boolean property in JavaScript?

HotTag

Archive