Hello I’d like to sort an array in a Rails Controller. I want to sort the array before I loop over it in the View
@projects = Project.all.sort #throws error
#and
@projects = Project.all
@projects.sort # throws error
throws this error: undefined method <=> for #<Project:0x101f70b28>
but when I query:
@projects.respond_to?('sort')
I get true
How can I sort the array? Should it be done in the View or in the Controller?
Any help is highly appreciated!
Ruby doesn’t know how to sort your project. You must specify the field to use for the sort. Example for
created_at:or
Alternatively, you can sort them at database level: