# Sorting posts

**URL:** <https://www.ruby-forum.com/t/sorting-posts/187300>\
**Category:** Rails\
**Created:** [April 21, 2010, 1:13pm UTC](https://www.ruby-forum.com/t/sorting-posts/187300 "2010-04-21T13:13:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [April 21, 2010, 1:13pm UTC](https://www.ruby-forum.com/t/sorting-posts/187300/1 "2010-04-21T13:13:10Z")

</div>

Hi,

I have a topic model which has\_many replies.

On my topic index page I would like to be able to sort the topics by  
the number of replies.

In my topic model I have defined

Topic.rb  
…  
has\_many :replies  
…  
def total\_replies  
replies.count  
end  
…

And in my controller I have tried

@topics = Topic.find(:all, :conditions =\> [“forum\_id = ?”,  
@forum.id], :order =\> “total\_replies DESC”)

but I get the error Mcolumn “total\_replies” does not exist

I know the column doesn’t exist, but the question is, how can I get  
the ‘total\_replies’ to behave as a column.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [April 21, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/sorting-posts/187300/2 "2010-04-21T13:31:19Z")

</div>

I think that’s you could use the counter\_cache feature:

class AddReplyCount \< ActiveRecord::Migration  
def self.up  
add\_column :topics, :replies\_count, :integer, :default =\> 0  
Project.reset\_column\_information  
Topic.find(:all).each do |t|  
t.update\_attribute(:replies\_count, t.replies.length)  
end  
end

def self.down  
remove\_column :topics, :replies\_count  
end  
end

class Topic \< AR  
has\_many :replies, :order =\> ‘replies\_count ASC’  
…  
end

class Reply \< AR  
belongs\_to :topic, :counter\_cache =\> true  
…  
end

El mié, 21-04-2010 a las 04:10 -0700, DanC  
escribió:\> Hi,

> has\_many :replies
> 
> but I get the error Mcolumn “total\_replies” does not exist
> 
> I know the column doesn’t exist, but the question is, how can I get  
> the ‘total\_replies’ to behave as a column.
> 
> Thanks,
> 
> Dan

–  
Juan José Vidal  
AgustínATICA - Sección de Telemática  
Universidad de Murcia  
Tlf: +34 868888742  
Fax: +34 868888337  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [April 21, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/sorting-posts/187300/3 "2010-04-21T13:33:05Z")

</div>

On 21 April 2010 12:10, DanC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @topics = Topic.find(:all, :conditions =\> [“forum\_id = ?”,  
> @forum.id], :order =\> “total\_replies DESC”)
> 
> but I get the error Mcolumn “total\_replies” does not exist
> 
> I know the column doesn’t exist, but the question is, how can I get  
> the ‘total\_replies’ to behave as a column.

You can’t - it’s a method on a model, and the DB knows nothing about it.  
But you can add a column that keeps the value of associated replies  
and then the query will work. Rails will do this for you with a  
“counter\_cache”.

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

(I’m still getting the hijacked domain, so I hope that’s the right link)

---

<div class="post-metadata">

**Author:** ![DanC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@DanC](https://www.ruby-forum.com/u/DanC)\
**Post date:** [April 22, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/sorting-posts/187300/4 "2010-04-22T22:00:21Z")

</div>

Michael P. wrote:

> On 21 April 2010 12:10, DanC [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > @topics = Topic.find(:all, :conditions =\> [“forum\_id = ?”,  
> > @forum.id], :order =\> “total\_replies DESC”)
> > 
> > but I get the error Mcolumn “total\_replies” does not exist
> > 
> > I know the column doesn’t exist, but the question is, how can I get  
> > the ‘total\_replies’ to behave as a column.
> 
> You can’t - it’s a method on a model, and the DB knows nothing about it.  
> But you can add a column that keeps the value of associated replies  
> and then the query will work. Rails will do this for you with a  
> “counter\_cache”.
> 
> [ActiveRecord::Associations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)  
> (I’m still getting the hijacked domain, so I hope that’s the right link)

You could also select it as an additional column (by getting it from the  
SQL count() function), although I don’t know how well ActiveRecord would  
deal with that.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
