# Will\_paginate and :order

**URL:** <https://www.ruby-forum.com/t/will-paginate-and-order/142256>\
**Category:** Rails\
**Created:** [July 5, 2008, 10:34am UTC](https://www.ruby-forum.com/t/will-paginate-and-order/142256 "2008-07-05T10:34:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [July 5, 2008, 10:34am UTC](https://www.ruby-forum.com/t/will-paginate-and-order/142256/1 "2008-07-05T10:34:04Z")

</div>

Hi!

I would like to code a kind of ‘forum’.

I’m using will\_paginate and I would like my topics to be order by the  
newest answer by topic.

For the moment I have:

- in my controller:

```auto
@categorie = Categoriemessage.find(params[:id])
@souscategories = Categoriemessage.find(:all, :conditions => ["parent_id
= ?", params[:id]])
@sujets = Message.paginate :per_page => 10, :page => params[:page],
                            :conditions => ["sujet_id = ? AND
categorie_id = ?", '0',params[:id]]
```

- in my helper:

```auto
def datedernierereponse(sujet_id)
  m = Message.find(:first, :conditions => ["sujet_id = ?", sujet_id],
:order => "datecreation desc")
  if m.nil?
    m = Message.find(sujet_id)
  end
    m.datecreation
end
```

Like this, my application works.  
@categorie is the current category of the list  
@souscategories are the children’s categories of @categorie.  
@sujets is the list of topics showed in the view.  
datededernierereponse(sujet\_id) is a function giving the date of the  
newest answer of a topic.

I don’t how to do:

- order @sujets by the newest answer.
- I would like @sujets to include the topics of the @souscategories,  
where ‘categorie\_id’ = @souscategories.id

My english isn’t so good, don’t hesitate to ask for more explanation or  
a reformulation.

Thanks !

---

<div class="post-metadata">

**Author:** ![chil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@chil](https://www.ruby-forum.com/u/chil)\
**Post date:** [July 9, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/will-paginate-and-order/142256/2 "2008-07-09T15:48:55Z")

</div>

The second problem is solved (include the topics of @souscategories in  
my request)

Here is the code:

@souscategories = Categoriemessage.find(:all, :conditions =\> [“parent\_id  
= ?”, params[:id]])  
categories = [@categorie] + @souscategories  
@sujets = Message.paginate :per\_page =\> 10, :page =\> params[:page],  
:conditions =\> {:sujet\_id =\> ‘0’, :categorie\_id =\> categories}

I still don’t know how to order the request by the date of the newest  
answer.  
(As i mentionned, I have a helper method giving the date of the newest  
answer, it just need the id of the topic.

I can give more information if it’s needed.
