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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:56:55+00:00 2026-05-31T13:56:55+00:00

I have Projelements , which are like: Milestone Task … etc … Each Projelement

  • 0

I have Projelements, which are like:

  • Milestone
  • Task
  • … etc …

Each Projelement can have one or more embedded Comments (thanks to Mongoid).

I want to be able to easily say:

Give me a list of Projelements sorted by when they were last updated

Where last_updated for a Projelement is the larger of:

  • this_projelement.updated_at OR
  • if this_projelement.comments.count > 0 then this_projelement.comments.last.updated_at

I can create a Projelement class method last_updated_at with the above logic.

But what I really want to end up with is doing a query of my Projelements, retrieving the last updated Projelement using the above logic, in the same way as you’d do a query using an attribute.

Previously, I tried using an attribute-only approach:

Tried using the updated_at attribute: this timestamp is updated if you update a Projelement‘s attributes, but is not updated if you update the embedded Comments. So I could manually update this_projelement.updated_at whenever a Comment is added. But the design feels brittle.

Also thought about having a separate attribute (e.g. last_updated_at) so that existing semantics of updated_at are not messed with. But design also feels brittle.

What would be the Rails way?

  • 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-31T13:56:57+00:00Added an answer on May 31, 2026 at 1:56 pm

    With MongoDB, there are no solution to order by some embedded document field.

    In your case, the solution to do a callback before_save in your Comment and update an attribute in your parent document seem the most reasonnable technic.

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

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
have Googled the cr*p out of this one so apologies if the answer is
Have a look at one of my websites: moskah.com The problem is that it
Have posting working wonderfully, and reading the response happily EXCEPT when one of the
Have a really weird one here. The app has two targets, just to be
have some headers like: HTTP/1.1 100 Continue HTTP/1.1 302 Found HTTP/1.1 200 OK HTTP/1.1
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
Have advancements in CPU design like dynamic instruction scheduling narrowed the performance gap between
Have any one tried to activate fancybox thumbnail gallery using a button or an

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.