Why delete_all works but not delele(nn)?

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.

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

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!

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.

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