views:

34

answers:

2

I have a many-to-many relationship like this:

class Event < ActiveRecord::Base
  has_many :calendar_events
  has_many :calendars, :through => :calendar_events
  # ...
end

I want to make it so that when I call some_event.destroy Rails deletes the association records from the calendar_events table. Based on the API docs, I'm assuming you do this...

class Event < ActiveRecord::Base
  has_many :calendar_events, :dependent => :delete_all
  has_many :calendars, :through => :calendar_events
  # ...
end

Is that the right way to do it?

+1  A: 

Yes. That will delete all the calendar events only.

Ryan Bigg
+1  A: 

You can also do :destroy instead of :delete_all. Delete all is faster for large sets of associated objects though.

See the Rails docs on has_many http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001789

Jared