クラスで単純なクエリを実行すると、返さItem.where(:id => id)
れますItemHelper
[
{
JDPA: null,
_id: "530e45f43f72fb3dee000001",
category: null,
created_at: "2014-02-26T19:52:20Z",
creator: "5170547c791e4b1a16000001",
detail: "Detail",
event_id: "52d958e73f72fb1e1f000001",
image_content_type: null,
image_file_name: null,
image_file_size: null,
image_updated_at: null,
question: null,
section: null,
title: "Title",
updated_at: "2014-02-26T19:52:21Z",
veh1: 5,
veh10: null,
veh2: 5,
veh3: 6,
veh4: null,
veh5: null,
veh6: null,
veh7: null,
veh8: null,
veh9: null,
version: null
}
]
:veh1..:veh10
asの配列を取得したい[5, 5, 6, null, ...]
。これを行うには多くの問題があります...
ヘルパーで JBuilder を使用することを考えましたが、今ではうまくいきません...
.only(:veh1..:veh10
ヌルを使用して削除するのと同じくらい簡単なことをするにはどうすればよいですか...おそらく配列を作成できます。
考え、コメント、私は愚かですか?? これは簡単なはずですか?私はここで初心者です。:)
私の答え
これは本当にもったいない気がします。これを行うより良い方法はありますか?? よりエレガントに??
def item_ppu(id)
item = Item.where(:_id => id)
newitem = item.map{|i| [i.veh1, i.veh2, i.veh3, i.veh4, i.veh5, i.veh6, i.veh7, i.veh8, i.veh9, i.veh10]}
h = Hash.new(0)
newitem[0].each { | v | h.store(v, h[v]+1) }
f = 2*h[2]#lots more math
return f
end