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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:25:26+00:00 2026-05-19T10:25:26+00:00

I have a data object which is composed_of three child instances of the same

  • 0

I have a data object which is composed_of three child instances of the same object. I need to calculate a value from that object and save it in the database. (It is later pulled form the db by another application.)

I’ve mocked out my class structure below, basically I want a method that will add together the Child values data_one and data_two, and store it into a third variable, total. Which I guess will be total_first, total_second and total_third in the db Parent table. Should I declare total as a Child attribute and use a callback to set it? Any help will be much appreciated – thank you! And apologies if this is a really obvious question…

class Child

  attr_reader :data_one, :data_two

  def initialize(data_one, data_two)
    @data_one, @data_two, 
  end

  def ==(other_child)
    data_one           == other_child.data_one           && 
    data_two           == other_child.data_two             
  end

  def Sdq.from_params(hash)
    Sdq.new(
      get_value(hash, :data_one),
      get_value(hash, :data_two),
    )
  end

  protected
    def Sdq.get_value(hash, key)
      hash.has_key?(key) ? hash[key] : hash[key.to_s]
    end
end 


class Parent < ActiveRecord::Base

  composed_of :first_child, :class_name => "Child", :mapping => [
    ["data_one_first","data_one"],
    ["data_two_first","data_two"]
  ]

  composed_of :second_child, :class_name => "Child", :mapping => [
    ["data_one_second","data_one"],
    ["data_two_second","data_two"]
  ]

  composed_of :third_child, :class_name => "Child", :mapping => [
    ["data_one_third","data_one"],
    ["data_two_third","data_two"]
  ]

end
  • 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-19T10:25:27+00:00Added an answer on May 19, 2026 at 10:25 am

    I can’t imagine why a parent will always have exactly two children, why not a many_to_x relation? depending if you need many to many. Hopefully not seeing the example.

    You must have your reasons.

    I would use call backs like you described. Maybe before_validations and have some validation on it. Note, that returning a non-true value in a callback can result in the record not saving.

    E.G If the condition is false, the method returns nil and can halt the save callback chain.

    def before_save
       if something_sometimes_is_false
           #do normal code
       end # could not run, which would return nil and break things
    end
    

    consider returning self at the end.

    def before_save
        if something_sometimes_is_false
            # do normal code
        end
        self # should always be a true value in an instance of a class. will not break call backs
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string data which I need to parse into a dictionary object.
I have a data object which has a base class with three derived classes,
I have a data structure object which is loaded from an XML file inside
HI all, I created a .net object (ex: A ) which contain data have
I have a fairly involved managed data model which has a central object with
In my application, I have three collection objects which store data. The data which
I have created data sources from my objects in my project, some of which
I have a data object -- a custom class called Notification -- that exposes
I have a data object that is deep-cloned using a binary serialization. This data
I have a repository layer that is responsible for my data-access, which is called

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.