@new_relationships = User.select('*')
.from("(#{@rels_unordered.to_sql}) AS rels_unordered")
.joins("
INNER JOIN relationships
ON rels_unordered.id = relationships.character_id
ORDER BY relationships.created_at DESC
")
``
produces a query that begins like this:
SELECT COUNT(*) FROM (SELECT .....
``
Why is it counting the records?? I haven’t asked for a count. I simply want to select all columns after the join:
So it looks like .any? affects the query? I would have thought rails would perform the query, get all the records into @new_relationships, and then count them. This is a surprising feature.
So it looks like .any? affects the query? I would have thought rails would perform the query, get all the records into @new_relationships, and then count them. This is a surprising feature.
Surprising possibly, but it saved a lot of processor time. Such is the magic of rails.
What rails is trying to avoid is loading 1000 objects from the db, just to check whether there is > 0 objects. If the query had already run, then it would just could the loaded objects.