I have a forum table that has a “last_post_id” column, which points to the last post made in a forum. How can I include that in my main forum query so the last topic is an object that I can access?
This isn’t working, but it helps convey what I’m after
Forum.all(
:include => {:posts => {:foreign_key => "last_post_id"}},
:order => "ancestry ASC, display_order ASC",
)
I’d like to be able to access the last post object using something like “forum.last_post.date”. Is this possible, what’s the cleanest solution?
The RAW sql should look something like this:
SELECT forums.*, last_post.*
FROM forums as forums
LEFT JOIN posts as last_post on last_post.id = forums.last_post_id
ORDER BY ancestry asc, display_order asc
1 Answer