# Sort table by child row property?

**URL:** <https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009>\
**Category:** Rails\
**Created:** [June 21, 2006, 10:48am UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009 "2006-06-21T10:48:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 10:48am UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/1 "2006-06-21T10:48:10Z")

</div>

I’m building a simple forum system which features a topics and a posts  
table. The topic model has a has\_many association with the post model,  
i.e.:

class Topic \< ActiveRecord::Base {  
has\_many :posts  
}

class Post \< ActiveRecord:Base {  
belongs\_to :topic, :dependent =\> true  
}

Now, I wish to sort the topics by the created\_at property of the last  
post, but I can’t for the life of me figure out how.

Any help would be greatly appreciated. I’ve spent the last two days  
trying to solve this.

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 11:44am UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/2 "2006-06-21T11:44:43Z")

</div>

hi there,  
thereÂ´s a couple of ways to do that in code… but that would involve a  
bit of sorting through arrays and stuff.  
i think the quickest way to accomplish what you want to do is to just  
put a last\_post\_created\_at field into your topic model and just update  
that each time someone writes a new post for that topic…  
kind regards,  
alexander

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 11:56am UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/3 "2006-06-21T11:56:32Z")

</div>

alexander wrote:

> i think the quickest way to accomplish what you want to do is to just  
> put a last\_post\_created\_at field into your topic model and just update  
> that each time someone writes a new post for that topic…  
> kind regards,  
> alexander

I’ve tried putting a last\_post\_id in the topics table, and reference it  
with:

class Topic \< ActiveRecord::Base {  
belongs\_to :last\_post, :class\_name =\> ‘Post’, :foreign\_key =\>  
‘last\_post\_id’  
}

But that caused the posts counter\_cache on topic.posts\_count to stop  
working and I couldn’t quite find the right place/hook to set the  
last\_post association ☹

Again, any advice is much appreciated.

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/4 "2006-06-21T12:58:29Z")

</div>

alexander wrote:

> oh.  
> iÂ´m sorry… thatÂ´s not what i meant.  
> my suggestion was something like this:
> 
> def whateversavesyourposts  
> code that saves your post here  
> @topic = Topic.find(@post.topic\_id)  
> @topic.last\_post\_created\_at(@post.created\_at)  
> @topic.save  
> end
> 
> i hope this is a bit clearer 🙂

I know 😉 But I thought: “Why save just the date if I could reference  
the entire post?” I know RForum, for instance, saves both the  
last\_post\_date and last\_post\_author\_id… that seemed a bit nonsensical  
if you can just save the last post 🙂

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/5 "2006-06-21T14:23:37Z")

</div>

Alex Griffioen \<info@…\> writes:

> I know 😉 But I thought: “Why save just the date if I could reference  
> the entire post?” I know RForum, for instance, saves both the  
> last\_post\_date and last\_post\_author\_id… that seemed a bit nonsensical  
> if you can just save the last post 🙂

It’s all about saving a join with the post tables. The query used to  
fetch the  
article “headers” doesn’t touch the posts table at all.

– Pazu

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/6 "2006-06-21T12:07:06Z")

</div>

oh.  
iÂ´m sorry… thatÂ´s not what i meant.  
my suggestion was something like this:

def whateversavesyourposts  
code that saves your post here  
@topic = Topic.find(@post.topic\_id)  
@topic.last\_post\_created\_at(@post.created\_at)  
@topic.save  
end

i hope this is a bit clearer 🙂

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 21, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/sort-table-by-child-row-property/64009/7 "2006-06-21T14:39:37Z")

</div>

Pazu wrote:

> It’s all about saving a join with the post tables. The query used to  
> fetch the  
> article “headers” doesn’t touch the posts table at all.
> 
> – Pazu

You’re saying it’s smart to store last\_post\_created\_at and  
last\_post\_author\_id in the topics table, right? I still don’t see why…  
I’d rather use eager loading to fetch the topic’s last post, but I can’t  
seem to assign topic.last\_post properly ☹
