I have a model called Category
, I can find the siblings of my current category by calling @category.parent.siblings
or Category.siblings_of(params[:id])
but as soon as I try to sort either of those requests I get method errors.
If I call Category.where(:parent_id => @category.parent.id)
nothing shows up but I get no errors.
If I call Category.siblings_of(params[:id]).sort('name')
I get the error:
undefined method `to_direction' for nil:NilClass
If I call Category.siblings_of(params[:id]).arrange(:order => 'name')
it says:
undefined method `name' for #<Array:0x007fb00de57b70>
Extracted source (around line #21):
18: %i.icon-plus
19: %li.nav-header a neighbour of:
20: - for neighbour in @siblings
21: %li= link_to neighbour.name, neighbour, :remote => true
I can get this to work fine as long as I don't sort the results at all, but I'd really like to know why sorting the results would bung everything up.
It's hard to know what's wrong with your where
query without seeing what's in your model. But the sort is probably because you're not trying the right methods. The normal way to sort with mongoid looks like:
Category.siblings_of(params[:id]).order_by(name: :asc)
Or see the docs for more syntax options.