Previously I ordered my posts as this:
@posts = Post.find(:all, :order => "created_at DESC")
But now I want to replace created_at
with a custom method I wrote in the Post model that gives a number as its result.
My guess:
@posts = Post.find(:all, :order => "custom_method DESC")
which fails..
It fails because you are asking your db to do the sorting.
@posts = Post.all.sort {|a,b| a.custom_method <=> b.custom_method}
Note that this becomes non-trivial when you want to start paging results and no longer wish to fetch .all
. Think about your design a bit before you go with this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With