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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:30:14+00:00 2026-05-24T20:30:14+00:00

My last post was on the best way to write a SQL query with

  • 0

My last post was on the best way to write a SQL query with conditions on a LEFT OUTER JOIN:
LEFT OUTER JOIN with conditions (where, order by)?

Now, I need to convert that good piece of SQL into a sweet Active Record Query (Rails 3). 😉

I have 2 Models:

Training has_many :training_histories

TrainingHistory belongs_to :training

How can I write a scope to get the results the SQL below retrieve ?

SELECT tc.id, tc.name, tc.order, 
th.id as history_id, th.finished_at, th.score
FROM trainings tc
LEFT OUTER JOIN training_histories th ON th.training_id = tc.id 
    and th.id =
    (SELECT th1.id from training_histories th1 where th1.training_id = tc.id
     and th1.finished_at is not null
     order by th1.finished_at desc limit 1)
WHERE tc.id > 4
AND tc.id < 8
GROUP BY tc.id
ORDER BY tc.order_by ASC, tc.id ASC

I want to retrieve all the records of TRAININGS and add the last valid (aka finished) associated record of TRAINING_HISTORIES.

Any suggestions ?

Thank you

  • 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-24T20:30:15+00:00Added an answer on May 24, 2026 at 8:30 pm

    For rails 3, try this:

    Training.select("trainings.id, trainings.name, trainings.order, 
                     trainings.id AS history_id, training_histories.finished_at,
                     training_histories.score").
             joins("LEFT OUTER JOIN training_histories 
                    ON training_histories.training_id = trainings.id 
                    AND training_histories.id = (SELECT th1.id FROM training_histories th1          
                                                 WHERE th1.training_id = tc.id
                                                 AND th1.finished_at IS NOT NULL
                                                 ORDER BY th1.finished_at DESC LIMIT 1)").
             where("trainings.id > 4 AND trainings.id < 8").
             group("trainings.id").
             order("trainings.order_by ASC, trainings.id ASC")
    

    Basically, you’re just converting your pre-written query into Rails 3 finder methods.

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

Sidebar

Related Questions

My last post was met by smarmy, unhelpful answers (comments), so i'll get right
I screwed up my last post. Lets see if I can get this one
I want to only grab the last post of all topics in a category
Guys, reffering to last post I'm trying to output data while template is instantiated
Ref. to my last post and sellibitze's comment to that post on passing array
I read this post last night, and I noticed it was from 2006. I
I'm building a basic forum where every post contains some text, first and last
Last year, Scott Guthrie stated You can actually override the raw SQL that LINQ
last time I asked how to populate a data structure here . Now I
What's the best way to set a creation date for an object automatically, and

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.