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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:18:35+00:00 2026-06-15T05:18:35+00:00

Suppose in my rails application I have a model Entry, which has a nested

  • 0

Suppose in my rails application I have a model Entry, which has a nested model Measures, such that each entry has_many measures (and measures belongs_to entry).

Each measure has its own incentive. Is it possible that Entry has an integer also named incentive, whose value is equal to the sum of all of its measures? How do you achieve this?

To me, it seems like this kind of becomes a two part question:
How to make a models field, upon submission, be defined based on another fields value? Then.. How to make a value, upon submission, be defined based on its nested models values?

  • 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-15T05:18:36+00:00Added an answer on June 15, 2026 at 5:18 am

    Try implement a callback using after_update in the model of the nested attributes, which updates its parent:

    class Measure < ActiveRecord::Base
      after_update :calculate_measure_sum
    
      ...
      private
    
      def calculate_measure_sum
        # calculate sum
        self.entry.save
      end
    end
    

    You might need to use the same method on the after_create callback as well.

    EDIT:

    After having read about touch in another question, I’d like to update my approach:

    class Entry < ActiveRecord::Base
      has_many :measures
      after_touch :calculate_measure_sum
    
      ...
      private
    
      def calculate_measure_sum
        # calculate sum
        self.entry.save
      end
    end
    
    class Measure < ActiveRecord::Base
      belongs_to :entry, touch: true
      ...
    end
    

    What happens here, is that everytime a Measure is created or edited, it informs its Entry that it is updated by calling its touch method. In the entry, we may use the callback after_touch in order to recalculate the sum of the measures. Note that the after_touch-callback is called on creation, deletion and modification of the measures.

    Compared to my previous approach, this approach puts the responsability on the Entry-objects, which is favourable from a design point-of-view.

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

Sidebar

Related Questions

Suppose I have a client side app that sends out requests to a rails
I have an application, built using MVC, that produces a view which delivers summary
I have a Rails application with a public REST API which authenticates API clients
Suppose I have two classes in a Rails application: class Subject < ActiveRecord::Base def
I'm working on a Rails 3.2.2 application which has JSON APIs and I use
I'm developing a new rails application which is supposed to be installed several times
I'm having working on a Rails application that is supposed to run on an
Suppose I want to have a blog with Rails 3 on my website and
In Rails 3.0.9 (Ruby 1.9.2, MySQL) I have a method that is supposed to
Suppose I have a pure virtual method in the base interface that returns to

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.