Rails SQL select, doesn't seem to work as expected with the CASE statement

Viewed 136

I'm trying to understand how to use the select method in queries to return custom columns.

I'm running Rails 5.2, database is postgresql.

m = Message.all.select("messages.*, CASE WHEN id > 30 THEN TRUE ELSE FALSE END AS above_30")

returns only the messages table with all its columns. How do I get the above_30 column, preferable eager loaded?

1 Answers

The above_30 is there, but Rails will not define an access for it because there is no column by that name in your schema.

You can define one yourself, or use m[:above_30] to access the "raw" attributes of the object as returned from the query.

Related