38
这可能是我的一个非常基本的疏忽,但我似乎无法想起一个简单的方法来删除通过has_many :through
加入的两个对象之间的关联。 IE:Active Record has_many:通过删除一个关联的记录
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
如果你有两个对象,tag
和photo
,你可以将它们只是做这个关联:
photo.tags << tag
那么,有没有一个同样简单与此相反?即:
photo.tags.remove tag
注意,这不会触发'before_destroy'或'在连接模型after_destroy'回调 - 到位delete'的'如果使用'destroy'你需要这个。 – PinnyM