我在Rails3中有一个类似这样的数组[["SWD", "150325006"], ["GOODS", "150325006"]]
。我想得到那些类型为“SWD”的数字。请检查下面的代码。
支付控制器.rb
class PaymentsController < ApplicationController
def payment
@payment=Vendor.new
respond_to do |format|
format.html
format.js
end
end
def check_type
if params[:commit]=="submit"
@vendor_type = PaymentVendor.where(:v_name => params[:v_name]).map{|v|[v.v_catagory ,v.Receipt_No]}
#@vendor_type = PaymentVendor.where(:v_name => params[:v_name]).pluck(:v_catagory)
else
@v_name=Vendor.where(:s_catagory => params[:payment][:s_catagory] ).pluck(:v_name)
end
end
end
@vendor_type给出的值是这样的
[["SWD", "150325006"], ["GOODS", "150325006"]]
请帮帮我。 最佳答案
@vendor_type = [["SWD", "150325006"], ["GOODS", "150325006"],["GOODS", "150325006sdfs"],["SWD", "150325006sdff"]]
output = []
@vendor_type.each do |i|
if i.first == "SWD"
output << i[1]
end
end
puts output
关于ruby-on-rails - 如何在Rails 3中获取数组元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29916935/