Ruby on rails 我可以传递多个参数以获得多个结果吗
我正在创建一个API,其中有一个名为invoices的表,其中customerType是一个列。customerType只能有四个可能的值,即PCT、RVN、INT或OTH 现在,我想传递如下URL:Ruby on rails 我可以传递多个参数以获得多个结果吗,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Ruby On Rails 3.2,我正在创建一个API,其中有一个名为invoices的表,其中customerType是一个列。customerType只能有四个可能的值,即PCT、RVN、INT或OTH 现在,我想传递如下URL: http://localhost:3000/api/quatertodate?group-by=customerNumber&customertype=RVN,INT http://localhost:3000/api/quatertodate?group-by=customerNumbe
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中传递多个参数时,当它应该通过执行内部或查询返回这些参数的组合结果时,它就会返回null
在控制器的索引方法中,我有:
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
注:我能找到的最接近的答案是。任何帮助或解释都将不胜感激 这是因为您试图查询customerType等于RVN,INT的发票 您可能需要对customertype参数执行拆分: 这将为您生成一个查询:
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` IN ('RVN', 'INT')
而不是:
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` = 'RVN,INT'
你的解决方案有效。但你的解释澄清了疑问D
SELECT `invoices`.* FROM `invoices` WHERE `invoices`.`customerType` = 'RVN,INT'