开发者

Count DIFFERENT results in a rails find call

开发者 https://www.devze.com 2023-02-18 04:07 出处:网络
Let\'s suppose I have a mod开发者_StackOverflowel with only two fields: name and street name. How would I find out the number of different *street names* in a controller method?This will return the t

Let's suppose I have a mod开发者_StackOverflowel with only two fields: name and street name.

How would I find out the number of different *street names* in a controller method?


This will return the total number of different street_names

Model.group(:street_name).all.count

This will return an ordered hash with a count of names on each street

Model.group(:street_name).count


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

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


Try this !

Model.find(:all,:select => 'DISTINCT street_name').size
0

精彩评论

暂无评论...
验证码 换一张
取 消