Home > OS >  How I can get unique data (*) from the table in rails
How I can get unique data (*) from the table in rails

Time:03-20

How I can get unique data (*) from the table in rails based on the city

ex :- my table as bellow id = 1 ,name = 'demo1' ,city = 'city1'

id = 2 ,name = 'demo2' ,city = 'city1'

id = 3 ,name = 'demo3' , city = 'city2'

i need output based on unique city, means output should be

id = 1 ,name = 'demo1' ,city = 'city1'

id = 2 ,name = 'demo2' ,city = 'city1'

CodePudding user response:

get all records based on the city then get once with uniq name keep in mind the id is unique as auto-generated by rails

Model.where(city: 'Paris').select('DISTINCT name')
  • Related