I have an array of objects, let's call it an Indicator
. I want to run Indicator class methods (those of the def self.subjects
variety, scopes, etc) on this array. The only way I know to run class methods on a group of objects is to have them be an ActiveRecord::Relation. So I end up resorting to adding a to_indicators
method to Array
.
def to_indicators
# TODO: Make this less terrible.
Indicator.where id: self.pluck(:id)
end
At times I chain quite a few of these scopes to filter down the results, within the class methods. So, even though I call a method on an ActiveRecord::Relation, I don't know how to access that object. I can only get to the contents of it through all
. But all
is an Array. So then I have to convert that array to a ActiveRecord::Relation. For example, this is part of one of the methods:
all.to_indicators.applicable_for_bank(id).each do |indicator|
total += indicator.residual_risk_for(id)
indicator_count += 1 if indicator.completed_by?(id)
end
I guess this condenses down to two questions.
- How can I convert an Array of objects to an ActiveRecord::Relation? Preferably without doing a
where
each time. - When running a
def self.subjects
type method on an ActiveRecord::Relation, how do I access that ActiveRecord::Relation object itself?
Thanks. If I need to clarify anything, let me know.
.all
, just use.scoped
like Andrew Marshall's answer indicates (Although in rails 4 it'll work with.all
). If you find yourself needing to turn an array into a relation you've gone wrong somewhere... – nzifnab