evanejin
evanejin

Reputation: 43

Elasticsearch - undefined method `as_indexed_json'

I'm able to do the bare bones keyword search with no issues in my app using elasticsearch, but as_indexed_json isn't working apparently and I have no idea why. I'm following this tutorial, and the section in question is almost halfway down.

Please let me know if I need to provide any additional info. I'm pretty new to this, so I apologize if this is a really dumb question.

Rails console:

2.2.4 :011 > Term.first.as_indexed_json

Term Load (0.4ms)  SELECT  "terms".* FROM "terms"  ORDER BY "terms"."id" ASC LIMIT 1

NoMethodError: undefined method `as_indexed_json' for #<Term:0x007fa6cd9a6408>
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.5/lib/active_model/attribute_methods.rb:433:in `method_missing'
from (irb):11
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/emplumb/.rvm/gems/ruby-2.2.4/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>' 

Model:

require 'elasticsearch/model'

class Term < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  def as_indexed_json(options = {})
    as_json(
      only: [:name, :definition, :etymology1, :etymology2, :uses, :romance_cognates, :notes1, :notes2, :quote1, :quote2]
    )
  end

end

Upvotes: 2

Views: 458

Answers (1)

Narasimha Reddy - Geeker
Narasimha Reddy - Geeker

Reputation: 3890

Based on that tutorial i hope you opened the irb using rails c.

But open console like this bundle exec rails c and try again

Upvotes: 2

Related Questions