I know that I can create an instance method to operate on a particular instance of class and a class method to operate on the class itself. I also know that I can define scopes to select a group of objects based on some criteria I specify.
However, in my case, I have a field date_of_last_approval
and I want to take an initial set of objects (pops) and then filter down until I find the oldest and then extract that field. With scopes and the AndAnd gem, I created a class method like this:
def self.oldest_approval_date(pops)
oldest = pops.which_are_compliant.oldest_state_change.first
oldest ? oldest.current_compliance_state_date : nil
end
which I can call as x = Pop.oldest_approval_date(user.pops)
.
However, it seems to me that I should be able to define a method that operates at the tail end of a chain of scopes, so I could have:
x = user.pops.oldest_approval_date
I found one article which seems to address this but I was surprised there is not more discussion around this. What is the general philosophy regarding designing methods like this and if it is acceptable what is the best form?