Coda Chang Coda Chang - 8 months ago 18
Ruby Question

Rails ActiveRecord select usage

I've been confused for a while, I follow the rails doc, and it say

Client.select("viewable_by, locked")
will only select only a subset of fields.

Now I got a model call
Goods


class Goods < ActiveRecord::Base {
:id => :integer,
:name => :string,
:translate_key => :string,
:created_at => :datetime,
:updated_at => :datetime,
:discount_json => :text,
:price_mapping => :text,
:goods_type => :string,
:reference_id => :integer,
:available => :boolean
}


When I try
Goods.select(:name)
, it works fine.
However, when I did

Goods.select(:name,:translate_key)


It threw
ArgumentError: wrong number of arguments (2 for 0..1)

Answer

If you want to pass multiple columns pass them in an array

Goods.select([:name, :translate_key])

This way you can still use symbols

Active record select