1

I have a array like this [["SWD", "150325006"], ["GOODS", "150325006"]] in rails 3.I want to get those numbers whose type is "SWD".Please check my code below.

payment_controller.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 is giving the value like this [["SWD", "150325006"], ["GOODS", "150325006"]]. Please help me.

4 Answers 4

5

You can use assoc:

array = [["SWD", "150325006"], ["GOODS", "150325006"]]

array.assoc('SWD').last    
#=> "150325006"
Sign up to request clarification or add additional context in comments.

2 Comments

@ spickermaan : after adding your line it is throwing error as "undefined methos last for nil class".
Did you change array to the name your variable has (probably @vendor_type)?
0
@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

Comments

0
â–¶ arr = [["SWD", "150325006"], ["GOODS", "150325006"]]
#⇒ [["SWD", "150325006"], ["GOODS", "150325006"]]
â–¶ arr.select { |e| e.first == 'SWD' }.map(&:last)
#⇒ ["150325006"]

2 Comments

@ mudasobwa : Your line is giving output as ["SWD","GOODS"] but i need only those numbers whose type is "SWD".
I have no clue what are you talking about. The code above returns exactly what expected.
0

Try this:

> array = [["SWD", "150325006"], ["GOODS", "150325006"]]
 => [["SWD", "150325006"], ["GOODS", "150325006"]] 
> array.select{|e| e[0] == "SWD" }.map(&:last)
 => ["150325006"] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.