How to add order by in select query in rails 2.1.1
How can I add order by birthdate desc
to the following query?
User have friends
User have profile
now = Date.today
end = now + 7.days
u= User.find(id)
@somevariable = u.friends.select{|f|
date = f.profile.birthdate开发者_JAVA技巧 if f.profile
selected = false
if date
if (now.month == date.month) && (date.day >= now.day && date.day <= end_day.day )
selected =true
elsif (end.month == date.month) && (date.day <= end.day && end_day.month != today.month)
selected = true
else
selected=false
end
end
selected }
Everything is working fine, except I need to order the results by birthdate
.
The really lazy way to do this is to slap a #sort_by
on the end - however, I would NOT. If you fix your code, then I might be able to order by during the initial find - however as is, it is unreadable.
精彩评论