E
E
ertaquo2013-06-05 08:54:56
Ruby on Rails
ertaquo, 2013-06-05 08:54:56

Deleting records with a has_and_belongs_to_many relationship?

Good day.
There are two models - Notes and Groups. They are linked by the has_and_belongs_to_many relationship. That is, in one group there can be many notes, and one note can be included in several groups.
Sobstna, the question is: is it possible to somehow simply delete all the notes that are included in only one specific group?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
No_Time, 2013-06-05
@ertaquo

Well, it seems like it's not so easy to do it through HABTM. More precisely, you do not need to do it through it at all. It's just a matter of ideology - HABTM does not support dependent: :destroy, it was originally designed so as not to delete any data within the collections themselves, but to delete only the links between them. Better make connections via has_many through: with dependent: :destroy and wrap each deletion in a catch block. Perhaps not the most elegant solution, but it will work) Or you can write your own scope in which you can already check whether this note exists anywhere else except in this group.
PS Sorry for the chaotic explanation, I hope I conveyed the meaning. If it is not clear, in the evening I can give an example code.

H
himik, 2013-06-05
@himik

group.notes.clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question