Created
April 5, 2011 21:35
-
-
Save bringhurst/904621 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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