8

Running: Ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0], Rails 3.2.0

I'm trying to get elastic search working through the TIRE gem across associations. For some reason I keep getting the following error/errors when performing a rake on a TIRE import or occasionally on a view:

Daves-MacBook-Pro:outdoor dave$ rake environment tire:import CLASS=Gear FORCE=true
[IMPORT] Deleting index 'gears'
[IMPORT] Creating index 'gears' with mapping:
{"gear":{"properties":{}}}
[IMPORT] Starting import for the 'Gear' class
--------------------------------------------------------------------------------
101/101 | 100% rake aborted!######################################
undefined method `last_name' for nil:NilClass

Tasks: TOP => tire:import

Here are my models: GEAR

class Gear < ActiveRecord::Base
  attr_accessible :title, :size, :price, :image_url, :sub_category_id, :user_id
  belongs_to :user
  belongs_to :sub_category

  validates :title, presence: true
  validates :size,  presence: true
  validates :price, presence: true
  validates :sub_category_id, presence: true
  validates :user_id, presence: true

  include Tire::Model::Search
  include Tire::Model::Callbacks


  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 18) do
      query { string params[:query]} if params[:query].present?
    end
  end

  def to_indexed_json
    to_json(methods: [:sub_category_name, :user_last_name, :user_first_name, :user_email])
  end

  def sub_category_name
    sub_category.name
  end

  def user_first_name
    user.first_name
  end

  def user_last_name
    user.last_name
  end

  def user_email
    user.email
  end
end

USER

class User < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation
  has_secure_password
  has_many :gears
  before_save :create_remember_token

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :first_name,  presence: true,
                          length:  {:maximum => 50 }
  validates :last_name,   presence: true,
                          length:  {:maximum => 50 }
  validates :email,       presence: true,
                          format:  {:with => email_regex},
                          uniqueness:  {:case_sensitive => false}
  validates :password,    presence: true,
                          confirmation: true,
                          length: {within: 6..40}

  def name
    first_name + " " + last_name
  end

  private

    def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
    end
end

Sub_Category

class SubCategory < ActiveRecord::Base
  attr_accessible :name
  belongs_to :category
  has_many :gears
end

What am I missing? Thanks.

Simon
  • 1,716
  • 1
  • 21
  • 37
DaveG
  • 1,203
  • 1
  • 25
  • 45
  • 2
    Update: Ok it turns out I had a few NIL values in my database that was the reason for the errors. Hopefully this can save a few people some time. – DaveG Mar 12 '12 at 21:37
  • 1
    Hi, good it's sorted out -- so the import process died when some author was `nil`?... Also, maybe you could reuse this: https://github.com/karmi/railscasts-episodes/commit/ee1f6f39002f32ad25134c81dd6de74ff1b708fa for indexing the associations without creating the special methods... – karmi Mar 13 '12 at 08:24
  • Yes, the import process would not work because some of my associations foreign keys were nil. It didn't seem to like that. Thanks for the suggestion, I'll take a look at it. – DaveG Mar 13 '12 at 16:55
  • 2
    Please see this answer: http://stackoverflow.com/questions/11692560/elasticsearch-tire-and-nested-queries-associations-with-activerecord/11711477#11711477 for a full-fledged example of how to work with ActiveRecord's association in Tire and elasticsearch. – karmi Jul 29 '12 at 17:43
  • Please answer your own question and mark the answer as accepted. – Patrick Oscity Apr 03 '13 at 19:16

1 Answers1

2

I had a few NIL values in my database that was the reason for the errors. Hopefully this can save a few people some time.

DaveG
  • 1,203
  • 1
  • 25
  • 45