48

This may be a very basic oversight on my part, but I can't seem to recall a simple method for removing an association between two objects joined via has_many :through. IE:

class Photo
  has_many :tags, :through => :taggings
  has_many :taggings, :dependent => :destroy
end

class Tags
  has_many :photos, :through => :taggings
  has_many :taggings, :dependent => :destroy
end

class Taggings
  belongs_to :photo
  belongs_to :tag
end

If you have two objects, tag and photo, you can associate them just by doing this:

photo.tags << tag

So, is there an equally simple opposite to this? ie:

photo.tags.remove tag
Andrew
  • 42,517
  • 51
  • 181
  • 281

1 Answers1

72

here's what you want:

photo.tags.delete(tag)
aguynamedloren
  • 2,273
  • 18
  • 23
  • 28
    Note that this will not trigger `before_destroy` or `after_destroy` callbacks on the join model - use `destroy` in place of `delete` if you require that. – PinnyM Jan 22 '14 at 15:00