Skip to content

Instantly share code, notes, and snippets.

@bringhurst
Created April 5, 2011 21:35
Show Gist options
  • Save bringhurst/904621 to your computer and use it in GitHub Desktop.
Save bringhurst/904621 to your computer and use it in GitHub Desktop.
class BuildingsController < ApplicationController
def destroy
@building = Building.find params[:id]
begin
@building.destroy
rescue
flash[:notice] = $!.message
end
respond_to do |format|
format.html { redirect_to buildings_url }
format.xml { head :ok }
end
end
end
class Building < ActiveRecord::Base
before_destroy :require_no_rooms
has_many :rooms
private
def require_no_rooms
self.errors.add :base, 'A building can not be deleted if it contains rooms.'
raise ActiveRecord::RecordInvalid.new self unless rooms.count == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment