I am creating an API where I have a table named invoices where customerType is a column. customerType can be of only four possible values IE. PCT, RVN, INT or OTH.
我正在创建一个API,其中有一个名为invoices的表,其中customerType是一列。 customerType只能是IE的四个可能值。 PCT,RVN,INT或OTH。
Now, I want to pass URLs like:
现在,我想传递以下网址:
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
But, the issue is whenever I pass a single customertype or no customertype at all, it works but whenever I pass multiple parameters in customertype it returns null when it should be returning combined result of those by performing internal OR query.
但是,问题是每当我传递单个自定义类型或根本没有自定义类型时,它都可以工作,但每当我在customertype中传递多个参数时,它应该返回null,因为它应该通过执行内部OR查询返回这些参数的组合结果。
In index method of controller I have:
在控制器的索引方法中我有:
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
NOTE: The closest answer I could find is * Link. Any help or explanation is much appreciated.
注意:我能找到的最接近的答案是* Link。任何帮助或解释都非常感谢。
1 个解决方案
#1
1
That's because you're trying to query Invoice
where customerType
equals RVN,INT
那是因为您正在尝试查询Invoice,其中customerType等于RVN,INT
You might need to perform split
on the customertype
param:
您可能需要在customertype参数上执行拆分:
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
This will generate for you a query:
这将为您生成一个查询:
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` IN ('RVN', 'INT')
Instead of:
代替:
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` = 'RVN,INT'
#1
1
That's because you're trying to query Invoice
where customerType
equals RVN,INT
那是因为您正在尝试查询Invoice,其中customerType等于RVN,INT
You might need to perform split
on the customertype
param:
您可能需要在customertype参数上执行拆分:
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
This will generate for you a query:
这将为您生成一个查询:
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` IN ('RVN', 'INT')
Instead of:
代替:
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` = 'RVN,INT'