0

I have a class Restaurant: Restaurant(id: integer, name: string, url: string, address: string

I would like to get all the different combinations of url and name and count them. How can I do that?

I've tried

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

but I get:

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

Leticia Esperon
  • 2,499
  • 1
  • 18
  • 40

1 Answers1

2

Did you try?

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

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

And also check this question Rails: select unique values from a column

Leticia Esperon
  • 2,499
  • 1
  • 18
  • 40
Ravi Mariya
  • 1,210
  • 15
  • 19
  • For more on counting uniq field https://stackoverflow.com/questions/36608/how-can-i-count-the-number-of-records-that-have-a-unique-value-in-a-particular-f – Ravi Mariya Jun 18 '18 at 18:50
  • 4
    Wrong answer, the above will FIRST query all records into memory, THEN count the unique combination inside Ruby, not the db. The right way to do this would be: `Restaurant.count("DISTINCT(url, name)")` – Niels Kristian Jun 11 '19 at 13:20