Searchkick search method argument error after ruby upgrade to 3.0.0

Update ruby version from 2.7.2 to 3.0.0, default Searchkick search behavior stopped working.

Error trace:

      ArgumentError in ProductsAvailabilitiesController#index

wrong number of arguments (given 2, expected 0..1)

searchkick (4.4.4) lib/searchkick/model.rb:43:in `searchkick_search'
...

Here is simple search method that works fine on ruby 2.7.2:

      def search_method(query, page)
  search_options = {
    fields: [:name, :address, :comment],
    match: :word_middle,
    page: page,
    per_page: 10
  }

  search(query, search_options)
end

Here is my Gemfile.lock:

      searchkick (4.4.4)
  activemodel (>= 5)
  elasticsearch (>= 6)
  hashie
elasticsearch (7.11.2)
  elasticsearch-api (= 7.11.2)
  elasticsearch-transport (= 7.11.2)
elasticsearch-api (7.11.2)
  multi_json
elasticsearch-transport (7.11.2)
  faraday (~> 1)
  multi_json

Is there a way ot fix this problem or it's a bug in gem sources?

1 ответ

In ruby 3.0.0 changelog you will find an updates about:

Changes in separation of positional and keyword arguments

As you see in your error trace, problem is in method searchkick_search. Let's check it's implementation in searchkick gem sources. It contains argument **options:

       def searchkick_search(term = "*", **options, &block)

In ruby 2.7.2 hashes are automatically converted to a keyword arguments. In ruby 3.0.0 not. If you you want to keep this behaviour - add "**" to search method argument.

Simply change in your sources this:

       search(query, search_options_hash)

To this:

       search(query, **search_options_hash)
Другие вопросы по тегам