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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:49:05+00:00 2026-06-14T01:49:05+00:00

I have a simple time-tracking app, that has projects, tasks, and entries. The setup

  • 0

I have a simple time-tracking app, that has projects, tasks, and entries.

The setup is straight-forward:

class Project < ActiveRecord::base
  has_many :tasks
  has_many :entries, :through => :tasks
end

class Task < ActiveRecord::base
  belongs_to :project
  has_many :entries

  default_scope order("name asc") # this causes problems
end

(Entry is entirely straight-forward, so I’ve left it out)

However, I’m running into trouble when trying to do custom ordering of entries selected from a project. Specifically, I’m trying to select the latest entry like so:

latest_entry = project.entries.order("created_at desc").first

But due to the :through => :tasks and the default_scope that Task has, the actual query that Rails executes becomes:

SELECT `entries`.* FROM `entries`
INNER JOIN `tasks` ON `entries`.`task_id` = `tasks`.`id`
WHERE `tasks`.`project_id` = 23
ORDER BY name asc, entries.date desc LIMIT 1 -- wrong order!

Note the ORDER BY clause – it includes the default_scope from Task, and only after that does it include the order I specified.

So basically, I don’t get the latest entry of all entries in the project, but only the latest in the first task.

Is there any way around that? Seems like there should be a way to ignore/negate default_scope on a through-model (without completely dropping the default_scope)

  • 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-14T01:49:06+00:00Added an answer on June 14, 2026 at 1:49 am

    How about reorder:

    latest_entry = project.entries.reorder("created_at desc").first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple problem that I've been stuck on for some time and
I have a simple rails search implemented in my app. At the time I
I have learned Perl and PHP.So I want to do simple real time project
I have made a simple design-time template that uses an enum that resides in
I'm writing a simple time tracking program to manage my own projects. I'm a
I have a simple time sheet application. It manages: Group of employees Employees A
this time i have a simple question: I have one table, related with himself
I have this simple code, using Joda-time. Works fine, but I have a problem.
I have a simple question, but I'm searching for longer time, but I always
I have a very simple MySQL table Name | Time | Count James |

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.