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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:20:36+00:00 2026-06-05T09:20:36+00:00

I haven’t found a single result related to my question when googleing, but it

  • 0

I haven’t found a single result related to my question when googleing, but it appears to me to be a fairly simple question:
Given is an article model and a comment model. Now, on the article show page, when listing all the article’s comments, each comment should have its chronological id within that article, not its overall id.

The associations and everything work fine but I have no idea how to implement the ‘relative’ comment_id without going the rather complicated way and manually adding an article_comment_id column to my comment model and dealing with that attribute in my controller.

Is there no easy method to call an objects id within its parent model that it belongs_to?

  • 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-05T09:20:38+00:00Added an answer on June 5, 2026 at 9:20 am

    As far as I know, there is not an automatic way to ask a record where it is indexed with its peers.

    The way I’ve done this is just to keep track of the index while iterating through the array.

    So for ERB:

    <% @article.comments.each_with_index do |comment, index| %>
        <div class="comment_id"><%= index + 1 %></div>
        <div class="content"><%= comment.content %></div>
    

    Or HAML:

    -@article.comments.each_with_index do |comment, index|
        .comment_id= index + 1
        .content= comment.content
    

    If your paginating, you’ll need to have index + 1 + (page*num_items) or something.

    You could save an index in the database, by adding a column such as iteration_id and then add an before-create filter to the Comment model like this:

    before_create do
      last_comment = article.comments.unscoped.order("iteration_id DESC").select('iteration_id').first
      self.iteration_id = last_comment ? last_comment.iteration_id + 1 : 0
      true
    end
    

    You’d need to do something when comments are destroyed as well.

    Hope that helps!

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

Sidebar

Related Questions

I haven't found any documentation on this or seen this done before, but is
Haven't seen many Geneva related questions yet, I have posted this question in the
Haven't seen anything about it here but it seems to solve one of the
I haven't used the STL much before, but I started to on this huffman
I haven't actually built an app yet, but I'm confused by documentation on bind-attr.
I haven't found a straight answer to how to make an animation in a
I haven't found a coherent answer to my quesion here :( My server's ISP
I haven't seen this question anywhere else, I hope someone can help. I have
Haven't seen anything, but I have seen that you can create albums in the
Haven't found what i'm looking for so far. I want to redirect all my

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.