计算Rails中不同的属性对

时间:2018-06-18 16:50:09

标签: ruby-on-rails

我有一个班级RestaurantRestaurant(id: integer, name: string, url: string, address: string

我想获得urlname的所有不同组合并计算它们。我怎么能这样做?

我已经尝试了

Restaurant.select(:url, :name).distinct.count

但我明白了:

No function matches the given name and argument types. You might need to add explicit type casts.

1 个答案:

答案 0 :(得分:1)

你试过吗?

Restaurant.pluck(:url, :name).uniq.count

更多http://api.rubyonrails.org/classes/ActiveRecord/Calculations.html#method-i-pluck

并查看此问题Rails: select unique values from a column