Re:Ruby 3.0 Preview 1
by mosselman on Sunday September 27, 2020 @02:51PM
(#24609168)
So if 'length' was the only option and I'd have to do ActiveRecord::Base.connection.execute('SELECT count(*) FROM users').values.first or something it would all be fine?
[ Parent
| on HN ]
Other ORMs separate building the query from executing the query, which is less ergonomic but more explicit. Drawing on Rust, https://diesel.rs/ shows how this approach would work:
users.select(count_star()).first(&connection)
which is really just a cleaned-up version of your suggestion.Of course, you could imagine an ActiveRecord-like API to make this nicer:
User.first{|t| t.count}
User.first{|t| t.count}.execute!
User.execute{|t| t.count}.values.first
# ...etc.
but the underlying problem is that building the query and executing the query are two discrete steps, Rails streamlines them instead of making the separation obvious, and when a system hides complexity it becomes harder to know what it's really doing.