I want to execute the following:
case 1:
x = Group.all[0].people
x.class
=> Array
x.order
fine.
🙂
case 2:
y = Person.all
y.class
=> Array
y.order
=> NoMethodError: undefined method `order' for #<Array:0x718c278>
🙁
Both x and y contain the same type of item – bunch of Person models.
Why can’t ruby order the array?
Thanks in Advance
Because in the Rails 3 the method all(GitHub) returns an array. It can’t be ordered with order method.
Although you may use the sort method do to the ordering.
The
peoplereturns anActiveRecord::Relationobject that can then be further scoped.Fortunatelly the
ActiveRecord::Relationhave theto_amethod that will return an array that you may sort.