我正在尝试在删除地址之前检查它是否未被使用。我的代码如下:
def destroy
@address = current_user.addresses.find_by_id(params[:id])
redirect_to user_addresses_path(current_user) if @address.nil?
if Organisation.find_by_address_id(params[:id]).count == 0 && Event.find_by_address_id(params[:id]).count == 0
@address.destroy
redirect_to user_addresses_path(current_user)
else
flash[:error] = "Cannot delete address because it is being used"
redirect_to user_addresses_path(current_user)
end
end
但是,这给了我一个错误:
undefined method `count' for nil:NilClass
我究竟做错了什么?