开发者

ActiveRecord :dependent confusion

I have the following AR models defined:

class Venue < ActiveRecord::Base
  has_many :events
end

class Act < ActiveRecord::Base
  has_many :events
end

class Event < ActiveRecord::Base
  belongs_to :venue
  belongs_to :act
end

What I want is if I delete a Venue or Act, any associated Events are also deleted. But if I delete an Event, the associated Venue and Act are NOT deleted. I've tried various :dependent variations, but开发者_如何转开发 nothing seems to be working.

This seems so simple. What am I missing?


This works for me. Deleting Venue/Act deletes all events it had, while deleting an event has no effect on Venue/Act it belonged to.

class Venue < ActiveRecord::Base
  has_many :events, :dependent => :destroy
end

class Act < ActiveRecord::Base
  has_many :events, :dependent => :destroy
end

class Event < ActiveRecord::Base
  belongs_to :venue
  belongs_to :act
end


I figured out the problem. I had to call destroy instead of delete on the parent object. The docs don't really make that clear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜