Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch, Tire & Associations

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.

like image 858
DaveG Avatar asked Mar 12 '12 17:03

DaveG


1 Answers

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.

like image 59
DaveG Avatar answered Oct 06 '22 18:10

DaveG