Forum: Ruby on Rails Extra Model Attributes from DB

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
PeteSalty (Guest)
on 2007-06-02 03:31
(Received via mailing list)
We have two models, Articles and Votes; it's the typical situation;
users can vote on articles so the models look something like this:

class Article < ActiveRecord::Base

  has_many :votes


class Vote < ActiveRecord::Base

  belongs_to :user
  belongs_to :article


(user isn't listed here because it's not important but you get the

What we want to do is actually get back the number of votes with the

The SQL for this is pretty straight forward and looks like:

    SELECT articles.*, COUNT( AS vote_count FROM
      articles INNER JOIN votes ON = votes.article_id

We'd like to be able to then do something like

Article.find(:all, :order => "vote_count DESC")


<%= Article.find(125).vote_count %>

problem is, we can't work out if it's even possible in Rails to make
the Article model use this SQL, or if there's another solution we're
Does anyone know if it can be done or could point us in the right

This topic is locked and can not be replied to.