For me, this is about .reverse vs .order('id DESC')
If I have
history = where(user_id: user).order('id DESC')
I can still perform another query like history.where(person_id: person)
If I have
history = where(user_id: user).reverse
I get: NoMethodError: undefined method 'where' for #<Array:0x000001031a3408>
Is this because these functions differ on the array type they should be used with (associative vs regular arrays)? What are my options?
ActiveRecord query methods (e.g.
where,order,joins, etc.) return anActiveRecord::Relation. This allows you to chain the methods to build a query, and the query is not executed until you use the actual data.Since
reverseis not a query method, it operates on the actual data in Ruby, and thus the query is executed so that Ruby may operate on the data, and it returns anArray. Since now you have only data (in the form of anArray) and not anActiveRecord::Relation, you cannot call another query method.