ertaquo
ertaquo

Reputation: 150

Deletion with has_and_belongs_to_many relation

I have two models, Note and Group, with has_and_belongs_to_many relations. Many notes can be in one group, one note can belong to many groups.

How can i delete all notes belongs to only one group?

UPD:

Thanks, but i don't want to delete all notes in group. I want to delete notes, that belongs only to one group.

Upvotes: 1

Views: 870

Answers (3)

tronmcp
tronmcp

Reputation: 346

See the following stackoverflow question and answer regarding destroying associated data; in addition there is a plugin to protect some of your associations from being destroyed, I have used this plugin successfully on rails 2.x but have not tried it on rails 3.x

Rails :dependent => :destroy VS :dependent => :delete_all

:protect plugin --> http://ruido-blanco.net/blog/rails-dependent-protect-plugin-english/

Upvotes: 1

Deepika
Deepika

Reputation: 826

You can also try this

@group.notes.destroy_all

OR

@group.notes.delete 

Upvotes: 1

Substantial
Substantial

Reputation: 6682

Instantiate your group, then call clear on the association.

@group.notes.clear

Here's the documentation.

Upvotes: 1

Related Questions