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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:25:19+00:00 2026-06-16T13:25:19+00:00

I have a Skill model, which has many prerequisites (or prereqs in my model),

  • 0

I have a Skill model, which has many prerequisites (or prereqs in my model), and I need to create a map N-levels deep of the prerequisites for a skill, because I need to pass this information to Javascript (with the gon gem in my controller) for some crazy, HTML5/KineticJS action that shows the birds eye view of the relationship between this skill and others.

Right now, the code below is working great to consistently map 5 levels deep.

delegate :url_helpers, to: 'Rails.application.routes'

def prereqs_map
  prereqs_array = [ self.title.to_s, url_helpers.skill_path(self), Array.new(prereqs) ]
  prereqs_array[2] = prereqs_array[2].map do |prereq1|
    prereq1 = [ prereq1.title.to_s, url_helpers.skill_path(prereq1), Array.new(prereq1.prereqs).map do |prereq2|
      prereq2 = [ prereq2.title.to_s, url_helpers.skill_path(prereq2), Array.new(prereq2.prereqs).map do |prereq3|
        prereq3 = [ prereq3.title.to_s, url_helpers.skill_path(prereq3), Array.new(prereq3.prereqs).map do |prereq4|
          prereq4 = [ prereq4.title.to_s, url_helpers.skill_path(prereq4), Array.new(prereq4.prereqs).map do |prereq5|
            prereq5 = [ prereq5.title.to_s, url_helpers.skill_path(prereq5), [] ]
          end ] # prereq depth 5
        end ] # prereq depth 4
      end ] # prereq depth 3
    end ] # prereq depth 2
  end   # prereq depth 1

  return prereqs_array
end

The problem, as you can see, is it’s super messy with nested maps and there’s no way that I can figure out to pass a depth parameter and have the method map out to whatever depth I pass it.

I figure there has to be a better way of doing this. Any thoughts?

  • 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-16T13:25:20+00:00Added an answer on June 16, 2026 at 1:25 pm

    Try this:

    class Skill
      # has many prereqs?
    
      def prereqs_map(depth = 5)
        mapping = Proc.new do |node, depth, current_level|
          [node.title.to_s, url_helpers.skill_path(node), node.prereqs.map {|prereq| mapping.call(prereq, depth, current_level+1)}] if current_level <= depth
        end
        mapping.call(self, depth, 0)
      end
    
    end
    
    >> skill.prereqs_map(5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Job model which can have many attachments. The Attachment model has
I have a Rails application which has an Employee model, a Skill model and
In my code I have a Category model which has many subcategories, and the
I have a item tracking system which has an Item model which has a
I have a to-many relationship in my CoreData model without an inverse relationship which
I have a many-to-many relationship in my Entity Framework 4 model (which works with
Afternoon all, I have a post model, which has a song name, song string
today i have talk with other friend ,he said he has logic programming skill
I have a User model which has_many experiments: class User < ActiveRecord::Base has_many :experiments,
I have this static method for a model, which is creating multiple records. This

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.