Sort table by child row property?

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.

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

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 :frowning:

Again, any advice is much appreciated.

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 :slight_smile:

I know :wink: 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 :slight_smile:

Alex Griffioen <[email protected]…> writes:

I know :wink: 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 :slight_smile:

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

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 :slight_smile:

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 :frowning:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs