Unique Rails Search Results

I am using Ernie Miller Metasearch in rails 3 application and have encountered a problem.

In my model

class Prospect < ActiveRecord::Base has_many :steps 

In my controller

 @search=Prospect.search(params[:search]) 

In my opinion (search form)

  <%= f.date_select :steps_updated_at_greater_than%> 

This works so that I get all Outlook where the steps have been updated from select_date. My problem is that if there were a few updated steps, I get duplicates in @search that I don't need when I move on to formatting search results in the view.

My question is the best way to remove duplicates from @search? Or prevent duplication from adding to @search in the first place?

+4
source share
3 answers

Do you want to:

 @search = Prospect.search(params[:search]) @search.relation.select("DISTINCT(step_id), prospects.*") 
+1
source
 @search=Prospect.search(params[:search]).select('DISTINCT step_id') 
+2
source
 @search = Prospect.search(params[:search]) @prospects = @search.relation.uniq 
+2
source

Source: https://habr.com/ru/post/1347268/


All Articles