ruby on rails 4 - Counting Total Number of Occurrences of Each Type in a Table Column -
i have table , looks this:
when save data csv file , upload rails application, app takes data , stores in database table called "fruitnames." redirects homepage shows data in table.
what i'm trying list number of each type of fruit above table. example, output in view might be:
- apple: 3
- orange: 1
- blueberry: 1
- pineapple: 1
and if user uploads csv contains different fruit avocado, list above should updated.
i'm not sure how go this. tried start off writing query, i'm not sure if it's right:
select classname, count(*) fruitnames group classname
the above query won't work in rails (i'd need write fruitnames.distinct.count('classname') or that). question is, how can count number of occurrences of each classname , have count show in view?
my guess involve creating def in controller , inside of it, allocating variable query , embedding variable in erb file loop of kind.
further clarification: when type "rails c" command line, opens irb. if type "fruitnames.distinct.count('classname')," find number of distinct values in classname 4 in case. starters, how can number show in view?
Comments
Post a Comment