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
感謝你的回答。僅供參考 - 我查看了before_destroy的API,但無法弄清楚如何使其停止銷燬操作。我可以像上面那樣離開,但有興趣瞭解更多關於您的建議如何工作的信息? – 2011-02-16 19:49:28