ruby-on-rails – Elasticsearch,Tire&Associations

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Elasticsearch,Tire&Associations前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
运行: Ruby 1.9.3p0(2011-10-30修订版33570)[x86_64-darwin11.2.0],Rails 3.2.0

我正试图通过各种协会的TIRE宝石进行弹性搜索.出于某种原因,我在TIRE导入或偶尔在视图上执行rake时不断收到以下错误/错误

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

这是我的模特:
齿轮

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

用户

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,length:  {:maximum => 50 }
  validates :email,format:  {:with => email_regex},uniqueness:  {:case_sensitive => false}
  validates :password,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

我错过了什么?谢谢.

解决方法

我的数据库中有一些NIL值是导致错误的原因.希望这可以节省一些人的时间.

猜你在找的Ruby相关文章