# Comments are not being nested

**URL:** <https://www.ruby-forum.com/t/comments-are-not-being-nested/222891>\
**Category:** Rails\
**Created:** [September 13, 2012, 8:22pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891 "2012-09-13T20:22:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 13, 2012, 8:22pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/1 "2012-09-13T20:22:29Z")

</div>

So for the convenience of all I have posted my code on gist. I am having  
issues using the ancestry gem to nest comments. the issue is that said  
comment does not nest under the parent comment, instead it is added to  
the end of the comments list.

I have a relationship of tasks to comments, where a task has many  
comments and a comment belongs to task. Your help is appreciated.

My code is posted here: [Help with nesting comments · GitHub](https://gist.github.com/3716160)

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 13, 2012, 9:58pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/2 "2012-09-13T21:58:13Z")

</div>

On 13 September 2012 19:22, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So for the convince of all I have posted my code on gist. I am having  
> issues using the ancestry gem to nest comments. the issue is that said  
> comment does not nest under the parent comment, instead it is added to  
> the end of the comments list.
> 
> I have a relationship of tasks to comments, where a task has many  
> comments and a comment belongs to task. Your help is appreciated.
> 
> My code is posted here: [Help with nesting comments · GitHub](https://gist.github.com/3716160)

Have a look at the Rails Guide on Debugging. That will show you ways  
of debugging your code. First home in on /exactly/ which section is  
failing so that rather than saying “comment does not nest under the  
parent comment, instead it is added to the end of the comments list”  
you can point to a couple of lines of code and say what it is doing  
wrong, so you can ask here again. In practice it is likely that  
having homed in on the lines of code you will see yourself what the  
problem is.

Colin

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 13, 2012, 10:28pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/3 "2012-09-13T22:28:34Z")

</div>

Colin L. wrote in post #1075895:

> On 13 September 2012 19:22, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So for the convince of all I have posted my code on gist. I am having  
> > issues using the ancestry gem to nest comments. the issue is that said  
> > comment does not nest under the parent comment, instead it is added to  
> > the end of the comments list.
> > 
> > I have a relationship of tasks to comments, where a task has many  
> > comments and a comment belongs to task. Your help is appreciated.
> > 
> > My code is posted here: [Help with nesting comments · GitHub](https://gist.github.com/3716160)
> 
> Have a look at the Rails Guide on Debugging. That will show you ways  
> of debugging your code. First home in on /exactly/ which section is  
> failing so that rather than saying “comment does not nest under the  
> parent comment, instead it is added to the end of the comments list”  
> you can point to a couple of lines of code and say what it is doing  
> wrong, so you can ask here again. In practice it is likely that  
> having homed in on the lines of code you will see yourself what the  
> problem is.
> 
> Colin

taking this into consideration and spending what seems like for ever  
scouring the internet and reading articles, even going over the tutorial  
I go thris from I have come to the conclusion that it is a few things.

One:

module CommentsHelper  
def nested\_comments(comments)  
comments.map do |comment, sub\_comments|  
render(comment) + content\_tag(:div, nested\_comments(sub\_comments),  
:class =\> ‘nested\_comments’)  
end.join.html\_safe  
end  
end

the code should display something like this when I rendered out  
comments,  
before it could do comments.map

{ #\<TreeNode id: 100018, name: “Stinky”, ancestry: nil\>  
=\> { #\<TreeNode id: 100019, name: “Crunchy”, ancestry: “100018”\>  
=\> { #\<TreeNode id: 100020, name: “Squeeky”, ancestry:  
“100018/100019”\>  
=\> {}  
}  
}  
}

except it doesn’t, I get

{ #\<TreeNode id: 100018, name: “Stinky”, ancestry: nil\>}  
{ #\<TreeNode id: 100019, name: “Crunchy”, ancestry: nil\>}  
{ #\<TreeNode id: 100020, name: “Squeeky”, ancestry: nil\>}  
{}

So there’s something wrong with the helper even when its the same as the  
example tutorial I was following

Two:

parent\_id is apparently an attribute of comments, how ever in the  
example I followed they did not have to assign it as a accessible  
attribute, where as I do - else I get an error - plus there is no column  
for parent\_id - as there ins’t suppose to be one, just ancestry and an  
index of ancestry - which I have tripple checked

How ever with all that said the code seems to work. so I kept moving on

- One of the other issues after playing with the code is the helper  
method.

if I take this:

module CommentsHelper  
def nested\_comments(comments)  
comments.map do |comment, sub\_comments|  
render(comment) + content\_tag(:div, nested\_comments(sub\_comments),  
:class =\> ‘nested\_comments’)  
end.join.html\_safe  
end  
end

and replace the nested\_comments(sub\_comments) with ‘hello’ I get

[commnet]  
hello  
[comment]  
hello

So with that new information. what else should I be looking for? I have  
red all the documentation I can, poured over examples, read he out put a  
thousand times. I have printed out comments, I have tried different  
styles such as @tasks.comments - as comments belong to tasks. and still  
I am lost.

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 13, 2012, 10:42pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/4 "2012-09-13T22:42:38Z")

</div>

On 13 September 2012 21:28, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > My code is posted here: [Help with nesting comments · GitHub](https://gist.github.com/3716160)  
> > > Colin  
> > > render(comment) + content\_tag(:div, nested\_comments(sub\_comments),  
> > > { #\<TreeNode id: 100018, name: “Stinky”, ancestry: nil\>  
> > > { #\<TreeNode id: 100018, name: “Stinky”, ancestry: nil\>}  
> > > parent\_id is apparently an attribute of comments, how ever in the
> 
> and replace the nested\_comments(sub\_comments) with ‘hello’ I get  
> I am lost.  
> OK, I misunderstood what you had already done. I think you are saying  
> it is a problem with the ancestry gem itself. Sorry I have not used  
> that. Anyone else?

Have you tried using the gem in the simplest way possible to make sure  
you understand how it is supposed to work?

Which version of Rails are you using?

Colin

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 13, 2012, 10:57pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/5 "2012-09-13T22:57:51Z")

</div>

On 13 September 2012 21:51, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > I am lost.
> 
> Yes. I am 100% sure I understand the full usage of the gem and have  
> tripple checked documentation, stack over flow and other blogs, its not  
> an issue with the gem, its an issue with my code.
> 
> it boils down to two things:
> 
> position\_id spazzes out if it is not an accessible attribute, which in  
> the documentation plus the examples I have read and followed - does not  
> need to be.

That could be an issue with the version of Rails you are using, I  
asked which one it is.

> ancestry seems to always be nil and because of this comments are not  
> nesting properly. so I am wondering how to make sure it gets set -  
> according to the documentation it should just be set when ever you reply  
> to a comment, because it uses the parent comments id to do so.
> 
> So I am wondering whats wrong with my code for this to not work \>.\<

Which line of your code is not doing the correct thing?

Colin

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 13, 2012, 11:48pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/6 "2012-09-13T23:48:02Z")

</div>

Colin L. wrote in post #1075908:

> On 13 September 2012 21:51, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > I am lost.
> > 
> > Yes. I am 100% sure I understand the full usage of the gem and have  
> > tripple checked documentation, stack over flow and other blogs, its not  
> > an issue with the gem, its an issue with my code.
> > 
> > it boils down to two things:
> > 
> > position\_id spazzes out if it is not an accessible attribute, which in  
> > the documentation plus the examples I have read and followed - does not  
> > need to be.
> 
> That could be an issue with the version of Rails you are using, I  
> asked which one it is.
> 
> > ancestry seems to always be nil and because of this comments are not  
> > nesting properly. so I am wondering how to make sure it gets set -  
> > according to the documentation it should just be set when ever you reply  
> > to a comment, because it uses the parent comments id to do so.
> > 
> > So I am wondering whats wrong with my code for this to not work \>.\<
> 
> Which line of your code is not doing the correct thing?
> 
> Colin

Sorry Colin, if it was a one line issue I wouldn’t bother with mailing  
lists, I would double check the code. this is “what’s wrong with my  
code” issue, hence why I posted gist. and ancestry works with 3.2 I know  
that much, but thanks for your help

any one else?

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 13, 2012, 10:51pm UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/7 "2012-09-13T22:51:35Z")

</div>

Colin L. wrote in post #1075904:

> On 13 September 2012 21:28, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > My code is posted here: [Help with nesting comments · GitHub](https://gist.github.com/3716160)  
> > > > Colin  
> > > > render(comment) + content\_tag(:div, nested\_comments(sub\_comments),  
> > > > { #\<TreeNode id: 100018, name: “Stinky”, ancestry: nil\>  
> > > > { #\<TreeNode id: 100018, name: “Stinky”, ancestry: nil\>}  
> > > > parent\_id is apparently an attribute of comments, how ever in the
> > 
> > and replace the nested\_comments(sub\_comments) with ‘hello’ I get  
> > I am lost.  
> > OK, I misunderstood what you had already done. I think you are saying  
> > it is a problem with the ancestry gem itself. Sorry I have not used  
> > that. Anyone else?
> 
> Have you tried using the gem in the simplest way possible to make sure  
> you understand how it is supposed to work?
> 
> Which version of Rails are you using?
> 
> Colin

Yes. I am 100% sure I understand the full usage of the gem and have  
tripple checked documentation, stack over flow and other blogs, its not  
an issue with the gem, its an issue with my code.

it boils down to two things:

position\_id spazzes out if it is not an accessible attribute, which in  
the documentation plus the examples I have read and followed - does not  
need to be.

ancestry seems to always be nil and because of this comments are not  
nesting properly. so I am wondering how to make sure it gets set -  
according to the documentation it should just be set when ever you reply  
to a comment, because it uses the parent comments id to do so.

So I am wondering whats wrong with my code for this to not work \>.\<

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 14, 2012, 10:06am UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/8 "2012-09-14T10:06:51Z")

</div>

On 13 September 2012 22:48, Kyle A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > position\_id spazzes out if it is not an accessible attribute, which in  
> > > to a comment, because it uses the parent comments id to do so.  
> > > code" issue, hence why I posted gist. and ancestry works with 3.2 I know  
> > > that much, but thanks for your help

It may not be a single line problem but it is certainly not necessary  
to look at the whole code. The first principle of debugging is to  
narrow down the problem. Have you determined whether the correct data  
are getting stored in the database? If not then concentrate on that  
first. If the problem is that the data are not getting stored  
properly then that narrows it down to the sections of code that get  
the data into the database, if the correct data are getting stored  
then the problem is a problem with viewing the data. Either way you  
have halved the extent of the code you need to look at.

To track down the problem with parent\_id then remove it from  
attr\_accessible and post the full failure message and the bit of  
development.log that corresponds to the action that generates the  
error.

Colin

Colin

---

<div class="post-metadata">

**Author:** ![kalons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kalons](https://www.ruby-forum.com/u/kalons)\
**Post date:** [September 14, 2012, 12:01am UTC](https://www.ruby-forum.com/t/comments-are-not-being-nested/222891/9 "2012-09-14T00:01:32Z")

</div>

So I have updated the gist [Help with nesting comments · GitHub](https://gist.github.com/3716160) to show more  
information
