With ActiveRecord has_many through relationships, how do I remove associations when saving objects

In Rails, how do you remove the has-many association through a link while preserving previously related objects? Is there an ActiveRecord way to do this or do I need to write SQL?

Is it also possible for objects to remain friends after the relationship is gone? [<- lame joke attempt]

+3
source share
3 answers

Found it myself. collections.delete and just make sure that dependents are not set to delete or destroy. For some reason, asking a question in public seems to lead to the fact that I get an answer after 10 minutes on my own.

+5
source

-, , , 10 .

, , , , , -, / .

+1
Class A  < ActiveRecord::Base
  has_many :b
end

Class B  < ActiveRecord::Base
  belongs_to :a
end

Abdelete - removes an association

Abdestroy - removes the association of associated objects (b)

+1
source

Source: https://habr.com/ru/post/1704507/


All Articles