diff --git a/app/models/event.rb b/app/models/event.rb index 0ac54592..92bfeb15 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -13,7 +13,7 @@ class Event < ActiveRecord::Base has_many :event_attachments, dependent: :destroy has_many :users, through: :event_users has_many :speakers, through: :event_users, source: :user - has_many :votes + has_many :votes, dependent: :destroy has_many :voters, through: :votes, source: :user has_many :commercials, as: :commercialable, dependent: :destroy belongs_to :event_type diff --git a/db/migrate/20140717110124_delete_events_without_user.rb b/db/migrate/20140717110124_delete_events_without_user.rb new file mode 100644 index 00000000..44f0fa68 --- /dev/null +++ b/db/migrate/20140717110124_delete_events_without_user.rb @@ -0,0 +1,16 @@ +class DeleteEventsWithoutUser < ActiveRecord::Migration + + def up + # User deletion without all the proper dependent: :destroy options might have left + # Events without a submitter (event_user association is deleted, but Event itslef is not) + Event.all.each do |event| + if event.users.blank? + event.destroy + end + end + end + + def down + raise ActiveRecord::IrreversibleMigration, 'Cannot reverse migration. Events deleted cannot be re-created' + end +end