2011-02-15 69 views
1

有沒有更好的方法來確保我不刪除關係的最後一條記錄?我覺得這應該通過驗證來完成,但不能阻止銷燬行動。確保至少有一條相關記錄仍然存在

僅供參考 - @organization存在,因爲嵌套的路線

class LocationsController < ApplicationController 
.... 
.... 
    def destroy 
    @organization = Organization.find(params[:organization_id]) 
    @location = @organization.locations.find(params[:id]) 
    count = Location.find_all_by_organization_id(@location.organization_id).count 
    if count > 1 
     @location.destroy 
     flash[:notice] = "Successfully destroyed location." 
     redirect_to @organization 
    else 
     flash[:notice] = "Could not destroy the only location." 
     redirect_to @organization 
    end  
    end 
end 

回答

相關問題