Counting DIFFERENT causes rails to find a challenge

Suppose I have a model with two fields: a name and a street name .

How to find out the number of different street names * in the controller method?

+3
source share
3 answers

This will return the total number of different street_names

Model.group(:street_name).all.count

This will return an ordered hash with counting names on each street

Model.group(:street_name).count
+4
source

why don't you do "count_by_sql" where you would use the query select count(*) from (select distinct(street_name) from <table_name>)

Other things you can do in ruby: <ModelName>.all.group_by(&:street_name).size

+3
source

Try it!

Model.find(:all,:select => 'DISTINCT street_name').size
+3
source

All Articles