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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:47:28+00:00 2026-06-12T13:47:28+00:00

Ok, I am looking to implement something fairly straight forward in a Rails app

  • 0

Ok, I am looking to implement something fairly straight forward in a Rails app I would say, but rails callbacks, transactions and caches are getting the better of me.

This is what I want to achieve:
Two objects project and task. Both have an attribute finish_date. Also a project :has_many => :tasks.

I want to tell the project that its own finish_date can never be earlier than the latest finish_date on its tasks. Whenever a task is updated, the project parent should check and adjust its own finish_date if required.

I have been messing with after_save callbacks and its not as simple as I imagined due to transactions and multiple versions of the same object in memory. Perhaps I am just doing it wrong.

How could I implement this in Rails?

  • 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-12T13:47:29+00:00Added an answer on June 12, 2026 at 1:47 pm

    How about the following Task model:

    class Task < ActiveRecord::Base
      belongs_to :project
    
      def after_save(task)
        self.project.finish_date = self.finish_date
      end
    end
    

    The project’s finish_date will be set to the task’s finish_date. The code is assuming, the currently saved tasked always has a later finished_date as any other. It that’s a wrong assumption, just add the appropriate if statement 🙂

    EDIT

    My suggestion from the comment would be to have the Task as listed above and overwrite the finish_date setter in Project like so:

    class Project < ActiveRecord::Base
      has_many :tasks
    
      def finished_date=(new_finish_date)
        self[:finish_date] = new_finish_date if self[:finish_date] < new_finish_date
      end
    end
    

    Since I’m coding from my head, I’m not sure whether the project instance should be saved after this. Perhaps using update_attributes in the setter might be a better idea.

    About the other solutions here

    @Joelios solution suggested what I did, only not with overriding the setter. The problem with that is, that Rails included methods always use the setter (even update_attributes and create). So with that, you had to call method update_task_date on your own which one might forget.

    @saverios solution suggests to use the Observer/Observable pattern. So, as I see it, Project would become an observer of all its Task objects. When a Task changes, it calls all its observers, i.e. its project, which itself updates its finish_date. It’s more of less the same what I suggested just with using a design pattern, hence making it more difficult.

    Another solution one could think about is to define a new function like update in Project which iterates through all connected tasks, looking for the newest finish_date. That update method would have to be called whenever a task updates. That would be really bad design because each update of one of n tasks would trigger n database queries and slow everything down.

    Hope that helps someone facing the same problem.

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

Sidebar

Related Questions

I'm looking to implement content negotiation on some resources in a Rails app. I'm
I am looking to implement something similar to the dashboard of facebook app where
I'm looking to implement something similar to the way JSONP callbacks work with WCF
I am looking to implement something that appears to have shopping cart functionality, but
I'm looking for a lib that would implement something similar to std::map and multimap,
I am looking to implement a continuous unit test running system, something I have
Looking to implement VisualSVN but I don't have my own server. I could install
I am looking to implement something in Objective-C for the IOS platform. I want
I am looking to implement something similar to facebook/gmail chat. I know they use
I was looking to implement something like the image below, and really have no

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.