And*_*rew 41 activerecord ruby-on-rails has-many-through
这对我来说可能是一个非常基本的疏忽,但我似乎无法想起一个简单的方法来删除两个通过连接的对象之间的关联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
Run Code Online (Sandbox Code Playgroud)
如果你有两个对象,tag并且photo你可以通过这样做来关联它们:
photo.tags << tag
Run Code Online (Sandbox Code Playgroud)
那么,这有一个同样简单的对立面吗?即:
photo.tags.remove tag
Run Code Online (Sandbox Code Playgroud)
agu*_*ren 66
这是你想要的:
photo.tags.delete(tag)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13202 次 |
| 最近记录: |