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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:45+00:00 2026-05-26T11:41:45+00:00

I was wondering, let’s say I have two models, discussions and post. Posts belong

  • 0

I was wondering, let’s say I have two models, discussions and post. Posts belong to discussions. I have a custom action in a Discussion model that acts to check the latest activity on an object by checking the creation date of its newest post. ie

 def latest activity
     self.posts.last.created_at || self.created_at
 end

Can i write a scope that orders the model’s objects by this method rather than, say, a column in the Discussion model?

 scope :latest_activity, #what to put here?
  • 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-26T11:41:46+00:00Added an answer on May 26, 2026 at 11:41 am

    is there a reason why you don’t want to alter the parent? Rails have a built-in method called touch for this exact thing. For example: belongs_to :discussion, :touch => :last_reply_at will make it so whenever a reply is changed(created/updated) the parent will have that field updated with Time.now timestamp. One benefit of this would be the ability to not query the children every time you want to see which discussion has the last reply.

    Have a look at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    EDIT

    Another way to do this would be to add a filter, for instance after_create on the post update a field with the current time on the discussion model.

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

Sidebar

Related Questions

I'm wondering if anyone knows how to accomplish the following: Let's say I have
I was wondering, If I have, let's say 6 javascripts includes on a page
I am wondering about fetching records using Model.get_by_key_name() vs Model.get_by_id() For example, let's say
I'm wondering let's say if i signup in a website and they have a
I'm wondering if the following DB schema would have repercussions later. Let's say I'm
I'm wondering if anyone has faced this issue. Let's assume I have two tables:
Wondering if this is possible: Let's say if I have a text input element
Wondering if this is possible in PHP Land: Let's say I have a class
I'm wondering if $_POST & $_GET can have issues with security. Let's say i
I'm wondering is it possible to reset id field, let's say now i have

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.