开发者

ActiveRecord :dependent confusion

开发者 https://www.devze.com 2023-03-15 17:31 出处:网络
I have the following AR models defined: class Venue < ActiveRecord::Base has_many :events end class Act < ActiveRecord::Base

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

精彩评论

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