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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:52:07+00:00 2026-06-03T14:52:07+00:00

Let’s say I have: class Foo < ActiveRecord::Base has_many :bar end class Bar <

  • 0

Let’s say I have:

class Foo < ActiveRecord::Base
  has_many :bar
end

class Bar < ActiveRecord::Base
  belongs_to :foo
end

Also assume that the Bar class has timestamp fields (the ones that are automatically updated when the record is updated).

Question: Is there existing functionality/method calls in Rails, ActiveRecord, or a gem somewhere that allows me to call a method that will tell me most recent value in the updated_at columns in any of the records, in any of the associations defined on the Foo class?

So, a theoretical foo.latest_change_to_associated_objects method should give me a timestamp of when the latest updated_at field is for any of my associated records (in this case, for any Bar record).

The simplest case, where you have a parent class and just one association is simple enough – a callback or two would do the trick. I’m curious about situations where a class has many associations defined. Is there a gem (or functionality already inside of ActiveRecord that I’m missing) that allows me to declare any/all of my associations to have this functionality?

Obviously I could do a SQL call to figure this out, but I don’t want to do that every time I check for the latest change. It seems like there should be a callback chain the automatically updates a latest_change_on_association field (or whatever you want to call it) on the parent when any associated record is changed, such that retrieving the latest change timestamp is a matter of checking a single field on the parent record.

Note: I looked into http://api.rubyonrails.org/classes/ActiveModel/Observer.html, but the problem there is that I’d have to write an observer for every class. It’d be great if the automation of creating the observers from a list of associations was already wrapped up in a gem or something.

  • 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-03T14:52:10+00:00Added an answer on June 3, 2026 at 2:52 pm

    You can use :touch to automatically update the updated_at timestamp (or any other timestamp field) of a parent record of a belongs_to when one of its associated records changes:

    Emphasis is mine:

    :touch

    If true, the associated object will be touched (the updated_at/on attributes set to now) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute.

    If you really want to use your last_association_updated_at column, you can:

    belongs_to :parent_class, :touch => :last_association_updated_at
    

    See the Rails Guide on Associations.

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

Sidebar

Related Questions

Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a model that handles recipes, and I want to
Let's suppose we have: Class Foo{ int x,y; int setFoo(); } int Foo::setFoo(){ return
Let's say I have the string: hello world; some random text; foo; How could
Let's say that I have a set of relations that looks like this: relations
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.