When I run a simple query of Item.where(:id => id)
in my ItemHelper
class and it returns
[
{
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
}
]
I want to get an array of :veh1..:veh10
as [5, 5, 6, null, ...]
. Having a lot of trouble doing this...
I thought of using JBuilder in my helper, but now I've blown things up...
How can I do something as simple as using .only(:veh1..:veh10
and remove the nulls...then I could probably make an array.
Thoughts, comments, am I being dumb?? Should this be easy? I'm a beginner here. :)
My Answer
This feels really sloppy. Is there a better way to do this?? More elegant??
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