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

  • Home
  • SEARCH
  • 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 8513857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:41:56+00:00 2026-06-11T04:41:56+00:00

c = Conversation.joins(:messages).random >> Conversation Load (8.1ms) SELECT `conversations`.* FROM `conversations` INNER JOIN `messages`

  • 0
c = Conversation.joins(:messages).random
>> Conversation Load (8.1ms)  SELECT `conversations`.* FROM `conversations` INNER JOIN `messages` ON `messages`.`conversation_id` = `conversations`.`id` ORDER BY created_at DESC, RAND() LIMIT 1

Count:

c.messages.count
>> (5.7ms)  SELECT COUNT(*) FROM `messages` WHERE `messages`.`conversation_id` = 74

Length:

c.messages.length
>> Message Load (1.2ms)  SELECT `messages`.* FROM `messages` WHERE `messages`.`conversation_id` = 82

How to not perform additional queries above? I thought I have already joined messages for the random conversation using INNER JOIN, and yet a new query is performed to count those results?

The second issue is attempting to get all joined messages with a specific user_id:

u = User with id 27 # has messages in 'c' results above
c.messages.where('user_id = ?', u.id).all
>> Message Load (5.3ms)  SELECT `messages`.* FROM `messages` WHERE `messages`.`conversation_id` = 82 AND (user_id = 27)

I used select to do this without additional queries:

c.messages.select { |msg| msg.user_id == u.id }
>> returns messages without query logged

I’d still appreciate how I might reduce or optimize these queries.

  • 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-11T04:41:58+00:00Added an answer on June 11, 2026 at 4:41 am

    Try using includes instead of joins to avoid the n+1 problem.
    Railscast #181 By Ryan Bates, http://railscasts.com/episodes/181-include-vs-joins?view=asciicast, explains the difference in an easy way.

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

Sidebar

Related Questions

If I want to show messages in a threaded conversation, which layout should I
I have a messages table that is structured somewhat like this: from | to
Take a typical left outer join scenario. We all know that the order of
I have the following SQL query that works like a champ: select u.id from
This seemed to spark a bit of conversation on another question and I thought
I want to make a conversation view with ListBox, but I don't know how
I'm looking to build a messaging system that handles conversation much like how Facebook
I am trying to implement a Session-Per-Conversation pattern in a JSF2-Spring-Hibernate Web App so
This question is a result of a lunch-time conversation with a co-worker...I've read questions
I'm trying to build a OneToMany association between the conversation entity and the sms

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.