srboisvert
srboisvert

Reputation: 12739

With ActiveRecord has_many through relationships how do I delete associations while keeping objects

In Rails how do you delete a has-many through association while retaining the formerly associated objects? Is there an ActiveRecord way to do this or I do need to write the SQL?

Also is it possible for the objects to remain friends once the relationship is gone? [ <-- lame joke attempt]

Upvotes: 3

Views: 5942

Answers (3)

user1331347
user1331347

Reputation: 11

Class A  < ActiveRecord::Base
  has_many :b
end

Class B  < ActiveRecord::Base
  belongs_to :a
end

A.b.delete - deletes the association

A.b.destroy - deletes the association an the Associated objects (b)

Upvotes: 1

Rayfleck
Rayfleck

Reputation: 12106

Somehow asking the question publicly seems to lead to me figuring the answer out in about 10 minutes on my own.

That's because writing it down traverses a different neural pathway, one that apparently has other connections/associations in your mind.

Upvotes: 1

srboisvert
srboisvert

Reputation: 12739

Found it myself. collections.delete and just have to make sure dependents are not set to be deleted or destroyed. Somehow asking the question publicly seems to lead to me figuring the answer out in about 10 minutes on my own.

Upvotes: 5

Related Questions