Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:12:45+00:00 2026-06-10T17:12:45+00:00

I have a forum table that has a last_post_id column, which points to the

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:12:46+00:00Added an answer on June 10, 2026 at 5:12 pm
    class Forum < ActiveRecord::Base
      has_many :posts
      has_one :last_post, :class_name => 'Post', :primary_key => :last_post_id
    end
    
    Forum.all(:include => [:last_post])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one is a table of forum threads. It has a
I have a form with the fields inside table cells. On the last column
I'm using version 4.1.3 I have a table migrated from access that has a
I have a table that has the user ID already in it, but some
Have a linking (or ref) table which has a dual primary key. Need to
I have a form that has multiple entries to the same table, the problem
I have a form that sends info into a database table. I have it
I have a table body that grabs it's rows (and form fields) via AJAX
I have a form with a table source, the form has a few fields
I have a form which lets you add names to the table and shows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.