# Why delete\_all works but not delele(nn)?

**URL:** <https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364>\
**Category:** Rails\
**Created:** [December 6, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364 "2007-12-06T23:58:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 6, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364/1 "2007-12-06T23:58:32Z")

</div>

Hi,

I’m sorry to post such a trivial question but…

I have a table for comments. And I can fill it normally:

@tab = Tab.find(params[:id])  
@tab.comments.create(:body =\> ‘This is the body 1’)

I am able to correctly see their contents this way:  
puts @tab.comments[0].body

I can also see its ID with:  
puts @tab.comments[0].id (22 for example)

So it is fine to add rows and check the right contents.

BUT

I am able to delete all the comments with:  
@tab.comments.delete\_all

However I am unable to delete a row in the table…  
@tab.comments.delete(22)

I’ve tried several things but as far as I know from some examples that  
should work.

I’m I doing something very wrong?

Thanks once again.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 7, 2007, 1:49am UTC](https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364/2 "2007-12-07T01:49:36Z")

</div>

On 6 Dec 2007, at 22:58, comopasta Gr wrote:

> I am able to delete all the comments with:  
> @tab.comments.delete\_all
> 
> However I am unable to delete a row in the table…  
> @tab.comments.delete(22)

On an association collection like you’ve got, delete\_all deletes all  
the rows in the collection, but delete deletes the object you pass in

- it doesn’t operate on ids.  
If you want to delete a comment by id, you need to use the delete  
method on the class (ie Comment.delete 22

Fred

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 7, 2007, 7:05am UTC](https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364/3 "2007-12-07T07:05:47Z")

</div>

> you need to use the delete method on the class (ie Comment.delete 22

Thank you so much for the explanation Fred. Indeed it worked like that.

Cheers!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 7, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364/4 "2007-12-07T13:40:46Z")

</div>

> I am able to delete all the comments with:  
> @tab.comments.delete\_all
> 
> However I am unable to delete a row in the table…  
> @tab.comments.delete(22)
> 
> I’ve tried several things but as far as I know from some examples that  
> should work.
> 
> I’m I doing something very wrong?
> 
> Thanks once again.

Actually @tab.comments will return an array.  
“delete” method is available for ruby Array class.

But there is no method called “delete\_all” for Array class.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [December 7, 2007, 2:39pm UTC](https://www.ruby-forum.com/t/why-delete-all-works-but-not-delele-nn/122364/5 "2007-12-07T14:39:19Z")

</div>

On 7 Dec 2007, at 12:40, Karthi kn wrote:

> > should work.
> > 
> > I’m I doing something very wrong?
> > 
> > Thanks once again.
> 
> Actually @tab.comments will return an array.  
> “delete” method is available for ruby Array class.
> 
> But there is no method called “delete\_all” for Array class.

Actually it returns an AssociationProxy that’s pretending to be an  
array.

Fred
