Y
Y
yezolux2014-11-28 00:46:48
Ruby on Rails
yezolux, 2014-11-28 00:46:48

RoR 4, Elasticsearch and searchkick. How to do a conditional search?

Hello.
To search in the project, I use Elasticsearch and the searchkick gem.
What is the correct way to use the where method? It is necessary that from the User table the search and output of results be with the condition, the field sex = 'male'.
Controller

if params[:query].present?
  query = params[:query]
  @user_array = User.search(query)

view
- @user_array.each do |user|
  ...

If you add
@res_male = User.where(sex: 'male') to the
controller and then do @user_array = @res_male.search(query), it will swear - undefined method `search' for #

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2014-11-28
@yezolux

@user_array = User.search(query, where: {sex: 'male'})

This is written in the gem's dock: https://github.com/ankane/searchkick#queries

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question