Simple model:
class hat
embedded_in :owner
field :color
end
class owner
embedds_one :hat
referenced_in :house
field :name
end
class house
references_one :owner
field :number
end
So simply puts, we have collection of houses that are associated to an owner, and the owner can have a colored hat.
I can simply sort the house by their number:
House.all.order_by( [[ :number, :asc ]])
But what I want is ordering the house, by the name of their owner, ideally I would like to write:
House.all.order_by( [[ :'owner.name', :asc ]])
But it does not work…
And even further I would like to be able to sort the houses by the color of the owner’s hat
House.all.order_by( [[ :'owner.hat.color', :asc ]])
Any idea how I can achieve this without rewriting everything if possible 🙂
Thanks
ALex
Dot notation is possible only for embedded documents, but you have 2 collections – houses and owners.
In one of the Owner records in MongoDB you have only field house_id, in one of the House records you dont have any connection to Owner model.
So the only way is to fetch all Houses and then to sort achieved collection using Enumerable#sort.