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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:05:58+00:00 2026-05-22T19:05:58+00:00

I have two models: Conversation and Message . A message belongs_to a conversation, and

  • 0

I have two models: Conversation and Message. A message belongs_to a conversation, and a conversation has_many messages:

# conversation.rb
class Conversation < ActiveRecord::Base
  has_many :messages
end

# message.rb
class Message < ActiveRecord::Base
  belongs_to :conversation
end

I have an interface that is going to list all the conversations, but I also want to include the “most recent” message associated with the conversation through eager-loading.

Do I need to create a new has_one or belongs_to relationship between the conversation and the message that will track the most recent message? Or is there a way I can do this through a scope using joins/includes?

Edit: The problem is that I don’t want a SQL query to be executed for every iteration of this loop:

# controller
@conversations = Conversation.all

# view
@conversations.each do |conversation|
  most_recent_message = conversation.messages.last
  # ...
end

Edit 2: I also tried to do the following, unsuccessfully. The goal was to add a last_message_id column to the conversations table, allowing it to reference the last added message.

# migration
add_column :conversations, :last_message_id, :integer

# conversation.rb
belongs_to :last_message, :class_name => 'Message'

# message.rb
after_create :set_last_message_on_conversation

def set_last_message_on_conversation
  self.conversation.last_message = self
  self.conversation.save!
end

However, that results in a SQL error since the last_message_id is null when the conversation is created. (I was hoping that it would all take place in a transaction, and that the last_message_id would be updated before the transaction completed, but I think it’s a MySQL/MyISAM issue where transactions are not used.)

ActiveRecord::StatementInvalid: Mysql::Error: Column ‘last_message_id’ cannot be null: INSERT INTO `conversations` (`last_message_id`, `updated_at`, `created_at`) VALUES (NULL, ‘2011-05-31 16:57:11’, ‘2011-05-31 16:57:11’)

  • 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-22T19:05:59+00:00Added an answer on May 22, 2026 at 7:05 pm

    You can use another association:

    # conversation.rb
    class Conversation < ActiveRecord::Base
      has_many :messages
      has_one :last_message, :class_name => "Message", :order => "created_at DESC"
    end
    
    # controller
    @conversations = Conversation.includes(:last_message)
    
    # view
    @conversations.each do |conversation|
      most_recent_message = conversation.last_messages
      # ...
    end
    

    Be warned that last_message and messages and considered independent associations. If you use conversation.messages, the messages will be loaded from the database. And if you change an attribute of last_message, it won’t affect messages.last until reload.

    Edit: Actually, according to the log, ActiveRecord still loads all the conversations’ messages. Probably because it can’t “LIMIT 1” like it does when it’s not eager loaded.

    So in terms of SQL requests, this solution seems to be equivalent to simply using Conversation.includes(:messages). But maybe Rails doesn’t process the extra results when using the has_one association.

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

Sidebar

Related Questions

I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have two models. order and line_item. class Order < ActiveRecord::Base has_many :line_items has_many
I have two models, Article and Post that both inherit from a base model
I have two models: Books and Authors. Books has_many Authors Authors belongs_to Books Problem
I have two models, Room and Image . Image is a generic model that
Let's say I have two models, Classes and People. A Class might have one
Say that I have two models- Users and Accounts. Each account can have at
I have two models Project and 'Task` where project has_many tasks and task belongs
I have two models that has a manytomany relationship with a 'through' table in
If I have two models. Model1 belongs_to Model2, and Model2 has_one Model1. Thus I

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.