English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

Descending Sort By in Model For Active Record Hash on Created_at attribute


If you have a couple collections from the database and you want to sort it without the help of Active Record, take a look at the sort_by method on Array type. I've used this before when I have a couple of collections which are slightly different but I need them in a chronological order.

@posts_group_a = Post.find :all, :conditions => ["user_id = ?", current_user.id] @posts_group_b = Post.find :all, :conditions => ["user_id = ?", friend_user.id]

#merge the two arrays here @posts = @posts_group_a + @posts_group_b

# notice the "-" is for descending order and the "to_i" casts the date time to an integer (required) @posts.sort_by {|post| - post.created_at.to_i}

Tagged w/ #active record #array #created_at #sort_byruby on rails