我可以传递多个参数以获得多个结果吗

Can I pass multiple parameters to get multiple result

我正在创建一个 API,其中我有一个名为 invoices 的 table,其中 customerType 是一列. customerType 只能有四个可能的值 IE。 PCT、RVN、INT 或 OTH。

现在,我想传递如下 URL:

http://localhost:3000/api/quatertodate?group-by=customerNumber&customertype=RVN,INT
http://localhost:3000/api/quatertodate?group-by=customerNumber&customertype=RVN,INT,OTH
http://localhost:3000/api/quatertodate?group-by=customerNumber
http://localhost:3000/api/quatertodate?group-by=customerNumber&customertype=PCT
http://localhost:3000/api/quatertodate?group-by=customerNumber&customertype=INT,PCT

但是,问题是每当我传递一个 customertype 或根本不传递 customertype 时,它都有效,但每当我传递多个参数时在 customertype 它 returns null 当它应该通过执行内部 OR[=35 返回那些的组合结果时=]查询。

controllerindex 方法中,我有:

def index
    @invoices=if params[:'group-by'].present?
        if params[:'group-by'].to_s == "customerNumber" 

            if params[:customertype].present?
                Invoice.where(customerType: params[:customertype])
            else
                Invoice.order('customerNumber')
            end
        end
    end 
    render json: {status: 'SUCCESS', messasge: 'LOADED QUATERLY INVOICES', data: @invoices}, status: :ok
end

注意: 我能找到的最接近的答案是 。非常感谢任何帮助或解释。

那是因为您要查询 Invoice,其中 customerType 等于 RVN,INT

您可能需要对 customertype 参数执行 split

def index
    @invoices=if params[:'group-by'].present?
        if params[:'group-by'].to_s == "customerNumber" 

            if params[:customertype].present?
                Invoice.where(customerType: params[:customertype].split(","))
            else
                Invoice.order('customerNumber')
            end
        end
    end 
    render json: {status: 'SUCCESS', messasge: 'LOADED QUATERLY INVOICES', data: @invoices}, status: :ok
end

这将为您生成一个查询:

SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` IN ('RVN', 'INT')

而不是:

SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` = 'RVN,INT'