I have a model Member that has a habtm relationship to buddies (also
members).
Now I wanted to sort them using sort, but this ends in an error
“undefined method <=>”! Where’s the problem here? Sorry, I’m still
beginning to learn Ruby.
I have a model Member that has a habtm relationship to buddies (also
members).
Now I wanted to sort them using sort, but this ends in an error
“undefined method <=>”! Where’s the problem here? Sorry, I’m still
beginning to learn Ruby.
Thanks
Josh
It sounds like you’re trying to sort ActiveRecord objects, but the
trouble with that is there is no “right” way to sort them–by first
name? last name? date at which they were created? backwards?
You have two options: sort them at the database level, or with Ruby.
Database level:
class Member < ActiveRecord::Base
has_and_belongs_to_many :buddies, :class_name => ‘Member’ …
etc …, :sort => ‘lastname, firstname’
end
This will fetch your associated ‘buddies’ in alphabetical order
(lastname, ascending) for each Member object.
Alternatively, you can do it in Ruby code like this (probably what
you tried):
This will sort by lastname. There’s a shortcut method in Ruby 1.8
called ‘sort_by’ as well. Robby R. recently posted a great
article at the oreilly ruby blog (O'Reilly Media - Technology and Business Training).
Unfortunately I can’t find his article there, so look here for a copy
instead: Swik - Swik News
+Hashes+with+Ruby+and+Enumerable/bmqu