# Acts\_as\_commentable: find Post by date of comment

**URL:** <https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289>\
**Category:** Rails\
**Created:** [August 30, 2008, 11:46am UTC](https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289 "2008-08-30T11:46:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tranquiliste](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tranquiliste](https://www.ruby-forum.com/u/tranquiliste)\
**Post date:** [August 30, 2008, 11:46am UTC](https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289/1 "2008-08-30T11:46:52Z")

</div>

Hello,

I am using acts\_as\_commentable (related to Post) and I would like to  
know how to get the list the posts ordered by date of comment (i.e.  
post with recent comment first).

in addition if a post has no comment, i would it to be inserted based  
on its creation date.

Thanks for your help

Nicolas

---

<div class="post-metadata">

**Author:** ![tranquiliste](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tranquiliste](https://www.ruby-forum.com/u/tranquiliste)\
**Post date:** [October 2, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289/2 "2008-10-02T23:17:39Z")

</div>

Here’s how I did it last night after about an hour of googling the  
shit out of it and coming up empty.

# First I load all the messages

@messages = Message.all

# Then I loop through them all, and if a message has a comment,

# I change the message’s created\_at field to the comments created\_at

@messages.each do |message|  
comment = message.comments.find(:first, :order =\> ‘created\_at  
DESC’)  
message.created\_at = comment.created\_at unless comment.blank?  
end

# Then I sort the messages by their (new) created\_at dates, which

don’t

# get saved so they don’t overwrite their real created\_at fields.

@messages.sort! { |b,a| a.created\_at \<=\> b.created\_at }

# Then I take the top 5 to use in my dashboard view.

@messages = @messages[0…4]

This works regardless of whether a Message (or Post in your case) has  
any comments. This looks a little hackish to me, so please, Rails  
gurus, have at it.

Hope this helps!  
Morgan Currie

---

<div class="post-metadata">

**Author:** ![tranquiliste](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tranquiliste](https://www.ruby-forum.com/u/tranquiliste)\
**Post date:** [November 24, 2008, 2:50am UTC](https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289/3 "2008-11-24T02:50:37Z")

</div>

## The Ruby on Rails web application framework has built up a tremendous head of steam over the last year. Fueled by some significant benefits and an impressive portfolio of real-world applications already in production, Rails is destined to continue making significant inroads in 2006. Simply put, Ruby on Rails is an open source tool that gives you the advantage of rapidly creating great web applications backed by SQL databases to keep up with the speed of the web.

kesha  
[Friends Dating](http://www.casualdate.net.au)

---

<div class="post-metadata">

**Author:** ![tranquiliste](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tranquiliste](https://www.ruby-forum.com/u/tranquiliste)\
**Post date:** [November 24, 2008, 4:14am UTC](https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289/4 "2008-11-24T04:14:29Z")

</div>

Message.all.sort\_by { |m| m.comments.blank? ? m.created\_at :  
m.comments.last.created\_at }[0…4]

---

<div class="post-metadata">

**Author:** ![tranquiliste](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tranquiliste](https://www.ruby-forum.com/u/tranquiliste)\
**Post date:** [November 22, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/acts-as-commentable-find-post-by-date-of-comment/147289/5 "2008-11-22T17:14:44Z")

</div>

Hi, it works fine witn ruby 2.x?? I’m very interested.

I read the it doesnÂ´t work with this version, see this comments:

[http://agilewebdevelopment.com/plugins/acts\_as\_commentable](http://agilewebdevelopment.com/plugins/acts_as_commentable)

and IÂ´m using rails 2.0.2
