Question

I would like to use hstore keys as table column headers. My approach is to simply map a rails query that will return all keys from multiple records and then print the uniq ones to the array.

I'll be building the table in Prawn, using both static and dynamic column headers...like this..but, this doesnt work of course.

 [["DATE", "LOCATION", "DAY OFF", "START", "END" + @users_options.select("properties").map { |k,v| ",#{k}" }]]

How can I iterate over the users logs, and output only uniq keys?

I just tried this...seems close...but not working yet

 a = []
 user.useroptions.select(:properties).collect{ |k,v| a << k }
Was it helpful?

Solution

I created a helper method:

def keys(user)
  keys = []
  user.useroptions.select(:properties).each do |opt| 
    a = opt.properties.keys
    keys << a
  end

  keys.flatten.uniq
end

This iterates through all hstore records, grabs the keys, then flattens the hash, and puts only the uniq values

OTHER TIPS

To finish this off, I moved the static array items into the helper and used the 'unshift' method...so that I spit out one array to the Prawn table builder.

def keys(user)
  keys = []
  user.useroptions.select(:properties).each do |opt| 
    a = opt.properties.keys
    keys << a
 end

 keys.flatten.uniq

end

keys.flatten.uniq.unshift("DATE", "LOCATION", "DAY OFF", "START", "END")

then I slip my helper in to the Prawn table

    [keys(@users_logs)] +

    ....table rows
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top