首页 > 解决方案 > 计算 Rails 中不同的属性对

问题描述

我有一堂课RestaurantRestaurant(id: integer, name: string, url: string, address: string

我想得到和计算它们的所有不同url组合name。我怎样才能做到这一点?

我试过了

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

但我得到:

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

标签: ruby-on-rails

解决方案


你试过了吗?

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

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

并且还要检查这个问题Rails: select unique values from a column


推荐阅读