Order on virtual attribute in has_many

Hi all,

Consider the following models:
class Sheep < AR::B
belongs_to :herd

def some_virtual_attribute
#returns something sortable
end
end

class Herd < AR::B
has_many :sheep do
def some_method
#do something useful
end
end
end

I want to be able to do:
some_herd.sheep.some_method

But the collection sheep should be sorted on the
some_virtual_attribute of Sheep.

I tried something along the line of:
has_many :sheep do
def ordered
sort {|a,b| a.some_virtual_attribute <=>
b.some_virtual_attribute}
end
end

This however returns an instance of Array on which the ‘some_method’
is not defined. How can I solve this?

With kind regards,
Harm

To get this to work, you’d need to return an AssociationCollection,
which is going to be tricky. Association extensions aren’t generally
combinable like this; I think you may be thinking about named scopes.

–Matt J.