0

我正在尝试在删除地址之前检查它是否未被使用。我的代码如下:

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

我究竟做错了什么?

4

1 回答 1

2

Organisation.find_by_address_id(params[:id])将返回一个对象,或者nil如果没有那个对象address_id不存在。

也许你的意思是Organisation.find_all_by_address_id(params[:id])

于 2012-05-11T21:33:38.253 回答