Using Arel in Rails - I'm looking for a way of creating an ActiveRecord::Relation
that effectively results in SELECT * FROM table
, which I can still manipulate further.
For example, I have a model that's split up into multiple categories, and I return counts for these in the following manner:
relation = Model.where(:archived => false) # all non-archived records
record_counts = {
:total => relation.count,
:for_sale => relation.where(:for_sale => true).count
:on_auction => relation.where(:on_auction => true).count
}
This works fine, and has the advantage of firing off COUNT
queries to MySQL, rather than actually selecting the records themselves.
However, I now need to include archived records in the counts, but relation = Model.all
results in an Array
, and I'm looking for an ActiveRecord::Relation
.
The only way I can think of doing this is model.where(model.arel_table[:id].not_eq(nil))
, which works, but seems slightly absurd.
Can anyone shed any light on this?