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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:25:53+00:00 2026-06-10T21:25:53+00:00

I would like the @comment1 to change to @comment2 by using the i in

  • 0

I would like the @comment1 to change to @comment2 by using the i in the 1..5 loop. I have the following code that is pretty repetitive. I am hoping to dry it up.

Hi,I am using acts_as_commentable_with_threading. I am basically looping through all comments and checking to see if that comment has children. If so, print out the children while checking to see if those children have children. So I plan on going a few levels deep, hence the @comment1,2,3, etc…How can I DRY this? Recursion some how? If not, I could maybe go a few levels deep and end the comment indentation at @comment5 for example.

EDIT!

Thank you Samiron!

Here is the updated helper function…

    def show_comments_with_children(comments)
    
     comments.each do |comment|
         
         yield comment
         if comment.children.any?
           concat <<-EOF.html_safe
                <div class="span7 offset1-1 pcomment">
           EOF
            show_comments_with_children(comment.children) {                 |x| yield  x } #Dont worry, this will not run another query :)
               concat <<-EOF.html_safe
                    </div>
               EOF
         end   
     end
  end

<div class="span7 offset1-1 pcomment">
<% @comment1 = comment.children%>
<% for comment in @comment1 %>
 <%= render "comment_replies", :comment => comment %>
            
<div class="span7 offset1-1 pcomment">
<% @comment2 = comment.children%>
<% for comment in @comment2 %>
<%= render "comment_replies", :comment => comment %>

<div class="span7 offset1-1 pcomment">
<% @comment3 = comment.children%>
<% for comment in @comment3 %>
<%= render "comment_replies", :comment => comment %>

                <% end %>
            </div>

….

<%(1..5).each do |i| %>
  <% @comment1 = comment.children%>
  <% for comment in @comment1 %>
    <%= render "comment_replies", :comment => comment %>
  <% end %>
<% 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-06-10T21:25:54+00:00Added an answer on June 10, 2026 at 9:25 pm

    Probably you are looking for instance_variable_set.

    # Following snippet is not TESTED. It is here to just demonstrate "instance_variable_set"
    <%(1..5).each do |i| %>
      <% instance_variable_set("@comment#{i}", comment.children) %>
      <% for comment in instance_variable_get("@comment#{i}") %>
        <%= render "comment_replies", :comment => comment %>
      <% end %>
    <% end %>
    

    But definitely this is not a recommendable approach. You can share your controller code and what you want to achieve in your view. There must be some way to make it properly DRY. In your post you are always getting comment.children. is it really?


    Actual Solution:

    Your view code will be like this

    #0th level is the top level
    <% show_comments_with_children(@comments, 0) do |comment, level|%>
       <!-- #Use level to differ the design for different depth-->
       <%= render "comment_replies", :comment => comment %>
    <%end%>
    

    and add this helper function show_comments_with_children in your helper function. Which will be.

    def show_comments_with_children(comments, level)
       comments.each do |comment|
           yield comment, level
           if comment.children.any?
               show_comments_with_children(comment.children, level+1) {|x, l| yield x, l} #Dont worry, this will not run another query :)
           end
       end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code and would like to change from using the live()
Using PHP , I would like to make a while loop that reads a
I am using a webGrid and would like to be able to change the
We have a data model that has some requirements. I would like to find
I am using Ruby on Rails 3 and I would like to change the
I have a list of method that I would like to call in a
I would like to get numbers of comments for each posts using mysql. I
I have a database with almost 100,000 comments and I would like to detect
I'm using Rails 2. I have resources nested like this: - university_categories - universities
I have a do-while loop that needs to log a message once (so it

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.