Elasticsearch, Tire & Associations
Asked Answered
B

1

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.

Balmy answered 12/3, 2012 at 17:26 Comment(5)
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.Balmy
Hi, good it's sorted out -- so the import process died when some author was nil?... Also, maybe you could reuse this: github.com/karmi/railscasts-episodes/commit/… for indexing the associations without creating the special methods...Stony
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.Balmy
Please see this answer: #11693060 for a full-fledged example of how to work with ActiveRecord's association in Tire and elasticsearch.Stony
Please answer your own question and mark the answer as accepted.Moriah
B
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.

Balmy answered 4/4, 2013 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.