2

I want to access multiple columns using Rails 3.But it gave me the following error.

Error:

ArgumentError (wrong number of arguments (2 for 1)):
  app/controllers/payments_controller.rb:13:in `check_type'

Check my below code.

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]).pluck(:type ,:Receipt_No)
           @vendor_type.each do |vendor|

            end
        else
            @v_name=Vendor.where(:s_catagory => params[:payment][:s_catagory] ).pluck(:v_name)
        end
    end
end

Actually i want to retrive data like below format.

@vendor_type=["Receipt_no":"type","Receipt_no":"type",.....]

Once these data will appear,I need how to access row values according to Receipt_No.Please help me to resolve this error.

0

3 Answers 3

3

Thanks to ActiveRecord >= 4 . pluck accepts multiple arguments so in

Rails 4: Your query will work

@vendor_type=PaymentVendor.where(:v_name => params[:v_name]).pluck(:type ,:Receipt_No)

Now as you are using Rails 3 which doesn't support multiple arguments to pluck then we can extend ActiveRecord::Relation itself like this:

put your file under config/initializers

# pluck_all.rb
module ActiveRecord
  class Relation
    def pluck_all(*args)
      args.map! do |column_name|
        if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
          "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
        else
          column_name.to_s
        end
      end

      relation = clone
      relation.select_values = args
      klass.connection.select_all(relation.arel).map! do |attributes|
        initialized_attributes = klass.initialize_attributes(attributes)
        attributes.each do |key, attribute|
          attributes[key] = klass.type_cast_attribute(key, initialized_attributes)
        end
      end
    end
  end
end

Now in your controller you can pass multiple arguments to pluck like this:

# payment_controller.rb:
@vendor_type=PaymentVendor.where(:v_name => params[:v_name]).pluck_all(:type ,:Receipt_No)

Now you can use pluck_all in whole app. Hope this helps ;)

EDIT: Try below code if plcuk_all not worked:

@vendor_type = PaymentVendor.where(:v_name => params[:v_name]).map{|v|[v.type ,v.Receipt_No]}

Reference for more info: http://meltingice.net/2013/06/11/pluck-multiple-columns-rails/

Sign up to request clarification or add additional context in comments.

10 Comments

@ Gagan: Your answer is always helpful and hope..it will help me a lot.I cannot implement right now..i will update you tomorrow..
@ Gagan : I did as per you defined but still it is giving me the error as-NoMethodError (undefined method `pluck_all' for #<ActiveRecord::Relation:0x20274 78>):.Please help me to resolve this error..
@rajat_474 : I have to checkout with Rails 3. Currently you can use jon snow answer or refer provided link. I will test it myself then will let u update. Till that time you can use other solution.
Ok...i am trying to use jon's method.
@ Gagan ,Do you have any other solution for this?
|
1

Your pluck(:type ,:Receipt_No) looks wrong, pluck have only one argument.

Also your type of data @vendor_type is wrong, Array don't have key, value pair.

Use map like this,

@vendor_type=PaymentVendor.where(:v_name => params[:v_name]).map { |i| [i.Receipt_No] }

7 Comments

pluck support multiple columns from rails 4 but only one in rails 3
@Ian : So Is there any other way to access multiple column in Rails 3?
Array don't have key, value pair. your expected @vendor_type is wrong.
use map to get your expected one.
|
0

In terms of making a rails 3 method that behaves the same as the Rails 4 pluck with multiple columns. This outputs a similar array (rather than a hashed key value collection). This should save a bit of pain if you ever come to upgrade and want to clean up the code.

See this tutorial which outlines a similar method that outputs a hash.

config/initializers/pluck_all.rb
module ActiveRecord
  class Relation
    def pluck_all(*args)
      args.map! do |column_name|
        if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
          "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
        else
          column_name.to_s
        end
      end

      relation = clone
      relation.select_values = args
      klass.connection.select_all(relation.arel).map! do |attributes|
        initialized_attributes = klass.initialize_attributes(attributes)
        attributes.map do |key, attribute|
          klass.type_cast_attribute(key, initialized_attributes)
        end
      end
    end
  end
end

Standing on the shoulders of giants and all

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.