
I needed to count how many rows where that column is equal to true (select count(*) from topics where [column_name] = true). Having the column name and that total count made up a key/value pair in my hash. If a column has no occurrences of true, there's no sense in including it in my graph because it would just be 0%.
So the problem was not knowing which columns to represent in the pie graph until runtime. I could get the data from each column and use many if/else statements but I wanted a more dynamic solution. You never know when the clients want one or fifty more columns...
After some research I thought I could use Object#instance_variable_get(symbol) on an ActiveRecord object but it wasn't so. Attributes in ActiveRecord models aren't instance variables. With some help and great explanations from the OC Ruby users' group, I proceeded using Object.send(symbol) to get an attribute value by passing its name as a symbol.
In the end, I ended up not displaying the graph because the small sample didn't give much value to decision makers. Yet. Maybe I can show the top 5 favorite topics instead of this:

--
My recommended books. At the bottom are my most completely read books. As you go up they become more of a reference for me.


1 comment:
Good post.
Post a Comment