ruby on rails 4 - Counting Total Number of Occurrences of Each Type in a Table Column -


i have table , looks this:

enter image description here

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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -