Closed romeerez closed 4 years ago
there is a hidden attribute __exists
for every record, but it's currently undocumented. It's set to 'true' for every record which got read from the database.
openrecord normally takes only field names as an argument for select()
. So Model.select('1')
will result in an SQL query simmilar to SELECT "1" from tablename
, where "1" would be a column name. But it will also check for parentheses and won't escape those strings. If you want the SQL result of 1
(the number), do the following: Model.select('(1) as foo')
or for SELECT row_to_json(tablename)
: Model.select('row_to_json(tablename)')
.
Be aware that by using SQL function calls openrecord can't return a valid model, only the raw result objects! no kidding
Thanks a lot for explanation, it was not working with just '1' because openrecord is smart, not stupid. And I can make cheap check by this code:
User.where(something).select('(1)').length // 0 - not exists, 1 - exists
But with row_to_json not quite clear, let me create another issue
ActiveRecord has
.exists?
method, OpenRecord doesn't, so I tried.select('1')
instead - got error.You know, openrecord is kinda for node.js, it's commonly used for api, and, on my opinion, is extremely important to be able do postges stuff like
SELECT row_to_json(*)
.No kidding, no ability to select custom string?