Ordering by a related classes created_at in Rails

I've got a model User with a related class (User has_many Posts, Post belongs_to User). I want to display Users in a list, with those who have posted most recently at the top. So basically I want to order the List of Users by the created_at date of their last post. How is the best way to query this in Rails 3?



Maybe try something like this.

class User < ActiveRecord::Base
  has_many :posts

  has_one :last_post, :order => 'created_at DESC', :class_name => "Post"

  scope :sort_by_last_post_desc, :include => :last_post, :order => ('posts.created_at DESC')

NOTE: not tested

I think I would just do this by pulling the Posts, ordering by created_at and doing a group by on the user_id.

Need Your Help

Twitters Card-Validator error: Failed to fetch page due to: OutOfHop

twitter twitter-card

Up until a couple of days ago our Twitter-Card was working fine and it was already approved.

Exec Command not Working in PHP

php iis-express

I am simply trying to run an executable file from a PHP script. Seems simple enough but it just will not work for me. I have tried both of these methods:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.