Search code examples
ruby-on-railsrubysphinxthinking-sphinx

Rails sphinx search undefined method `stringify_keys!'


I try to search in my mysql database using thinking_sphinx gem. But I am having some troubles:

I get the error:

1.9.3-p429 :002 > Article.search "diesel"

**NoMethodError: undefined method `stringify_keys!' for "diesel":String**

    from /home/pavel/.rvm/gems/ruby-1.9.3-p429/gems/meta_search-1.1.3/lib/meta_search/builder.rb:86:in `build'
    from /home/pavel/.rvm/gems/ruby-1.9.3-p429/gems/meta_search-1.1.3/lib/meta_search/searches/active_record.rb:43:in `metasearch'
    from (irb):2
    from /home/pavel/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /home/pavel/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /home/pavel/.rvm/gems/ruby-1.9.3-p429/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

What am I doing wrong?

my app/indices/article_index.rb:

ThinkingSphinx::Index.define :article, :with => :active_record do
  indexes :ART_ARTICLE_NR
end

(also, I have non standard db, field is uppercase)

gemfile:

gem 'thinking-sphinx'
gem 'mysql2'

Why am I getting this error?

Also when I try:

1.9.3-p429 :003 >  Article.search(ART_ARTICLE_NR: "diesel")

I get:

Killed

I have sphinx 2.0.4.


Solution

  • If you look at the stack trace you see that actually MetaSearch gives an error, not ThinkingSphinx. The issue is that both gems add search method to ActiveRecord::Base and you get a name conflict. Actually you call search method that belongs to MetaSearch and requires quite different params. To get rid of the name conflict, add thinking-sphinx below meta_search in Gemfile. After this you would be able to use Article.search to run ThinkingSphinx search and Article.metasearch to run MetaSearch one without errors.