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 834585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:41:40+00:00 2026-05-15T04:41:40+00:00

I have two models/tables in my Rails application: discussions and comments. Each discussion has_many

  • 0

I have two models/tables in my Rails application: discussions and comments. Each discussion has_many comments, and each comment belongs_to a discussion. My discussions table also includes a first_comment_id column and last_comment_id column for convenience and speed. I want to be able to call discussion.last_comment for the last comment model, but the following (in my discussion model) isn’t working to make this happen:

has_one :first_comment, :class_name => "Comment"
has_one :last_comment, :class_name => "Comment"

When I call discussion.last_comment, the following SQL is run:

SELECT * FROM `comments` WHERE (`comments`.discussion_id = 1) LIMIT 1

It’s using the discussions.id column to join against comments.discussion_id, when I want it to join discussions.last_comment_id against comments.id.

Am I using the wrong type of association here? Thanks for your help!

  • 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-05-15T04:41:41+00:00Added an answer on May 15, 2026 at 4:41 am

    Try a belongs_to, rather than a has_one.

    The naming might be odd, but the difference between the two is really just about which of the tables holds the foreign key column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, user and group. I also have a joining table groups_users.
In rails application I have two models: Food and Drink . Both food and
I have two tables, users{name,id,age_range_id} and age_ranges{id,range_name} . There are also two models, controllers
So I have two rails models - user and role - each specified with
I have a Rails 3.1.1 application with the following models: Company Member The two
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
If i have two tables Books, CDs with corresponding models. I want to display
I have a n...n structure for two tables, makes and models . So far
I have two models, say Product and Bundle. products table is related to product_prices
I am creating a site in Ruby on Rails, I have two models a

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.