jeudi 14 janvier 2016

Best way to remove all associations has_and_belongs_to_many without deleting the records

I have a user model that has_and_belongs_to_many :clubs so I can seen e.g. clubs.users and it returns all the users for that club. What I want to do is remove/break the associations all the users for that club. I want the users to still exist but just not be associated to that club anymore.

I am aware of e.g. club.users.delete_all but that ended up deleting the users as well. I cannot think of another way to approach this for removing all of the records.

Worth noting I know you can do club.users.delete(user) I am unsure how to pass in all the users associated to that club to that method. Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire