Active Record has_many : 通过删除一个关联的记录 [英] Active Record has_many :through remove one associated record

查看:16
本文介绍了Active Record has_many : 通过删除一个关联的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说可能是一个非常基本的疏忽,但我似乎想不起来有一个简单的方法来删除通过 has_many :through 连接的两个对象之间的关联.IE:

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

如果你有两个对象,tagphoto,你可以通过这样做来关联它们:

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

推荐答案

这里有你想要的:

photo.tags.delete(tag)

这篇关于Active Record has_many : 通过删除一个关联的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆