Kievbuy
6/7/2018 - 9:54 AM

Solr filter


5
down vote
accepted
First add a scope to your User model:

scope :confirmed, where("confirmed_at IS NOT NULL")
Then you have just to add the scope before the search

@search = User.confirmed.search do  
  fulltext params[:search]
  paginate :page => params[:page], :per_page => 10
end 
EDIT:

Indeed, after a test, the solution above don't seem to work. The scope seems to be ignored. There's another solution:

In your User model you probably have a method like this:

searchable do 
  text :lastname, :firstname
  ...
end
You have to add a condition to searchable:

searchable if: proc { |user| user.confirmed? } do 
  text :lastname, :firstname
  ...
end
EDIT 2:

There is another way if sometimes you want confirmed users and sometimes all users, but you need to modify you model too.

In you User model:

searchable do 
  text :lastname, :firstname
  boolean :confirmed do
    confirmed_at != nil
  end
  ...
end
Then in your controller

@search = User.search do  
  fulltext params[:search]
  with(:confirmed, true) # true if you want confirmed users, false for unconfirmed and if you want all users you don't write this line
  paginate :page => params[:page], :per_page => 10
end 
I hope this help