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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:58:22+00:00 2026-06-14T06:58:22+00:00

Imagine I have an object model: A Blog has many Articles, and an Article

  • 0

Imagine I have an object model:

A Blog has many Articles, and an Article has many Comments

Imagine also that I have two blogs, Blog A and Blog B.

Blog A - Article id 1 - Comment id 1 "fun stuff"
       - Article id 2 - Comment id 2 "cool"

and

Blog B - Article id 3 - Comment id 3 "no fun"

I need to compare the object graph for Blog A and Blog B, and update Blog B based on the value of objects in Blog A.

In this case, Blog B should change Comment 3 to be “fun stuff”, and instantiate new objects with values identical to Article 2 and Comment 2.

Recursively walking the graph is the obvious solution, but the logic gets convoluted. I’d rather not re-invent the wheel…is there a pattern or process to do this?

I’m using Ruby/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-14T06:58:23+00:00Added an answer on June 14, 2026 at 6:58 am

    After reading more about the visitor pattern, I decided a Rubyish variant of it was the most appropriate approach to solving this problem.

    The visitor pattern allows you to separate the algorithm for walking the hierarchy, from the code to execute on each node in the hierarchy. A more functional approach to this using map or inject/fold is possible…but as I want to reuse the operators, it seemed easier to break them into separate classes.

    The hierarchy is implemented in each model, which should define a “children” method that returns children.

    Below is my implementation, based off of various references, I may wrap it up into a gem.

    module Visitable
      def accept visitor
        child_vals = []
        if respond_to?(:children)
          children.each do |child|
            child_vals << child.accept(visitor)
          end
        end
        val = visitor.visit(self)
        child_vals.any? ? val + child_vals : val
      end
    end
    
    class Survey
      attr_accessor :name, :children
    
      include Visitable
    
    end
    
    class Category
      attr_accessor :name, :children
    
      include Visitable
    
    end
    
    class Question
      attr_accessor :name
      include Visitable
    end
    
    s = Survey.new
    s.name = 's1'
    c = Category.new
    c.name = 'c1'
    c2 = Category.new
    c2.name = 'c2'
    q = Question.new
    q.name = 'q1'
    q2 = Question.new
    q2.name = 'q2'
    
    c.children = [q]
    c2.children = [q2]
    s.children = [c,c2]
    
    class ReturnVisitor
      def visit obj
        obj.name
      end
    end
    
    s.accept(ReturnVistor.new)
    -> ['s1', ['c1', ['q1'], ['c2', ['q2']]]]
    
    # "poorly implemented lisp"?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you have an object foo that you saved as saved.file.rda as follows: foo
Imagine you have an entity that has some relations with other entities and you
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
Imagine I've got a data object that makes sense in an OO model, but
Imagine I have a abstract FriendEvent model which has several different concrete implementations, ie.
I have in my model an object, that when modified requires a large number
Imagine, I have News models with many text fields class News(models.Model): title = models.CharField(max_length=255)
I currently have a metric model, that can take two operands and perform a
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine you have class A which has code which runs as method M. And

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.