2012-03-12 15 views
8

実行:ルビー1.9.3p0(2011年10月30日リビジョン33570)[x86_64の-darwin11.2.0]、Railsの3.2.0Elasticsearch、タイヤ&協会

私は弾性検索が通過働いて取得しようとしています関連を通じてTIRE宝石。ここで

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 

は私のモデルです: 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+\-.][email protected][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 

何が欠けていますか?ありがとう。

+2

更新: これで、エラーが発生したデータベースにいくつかのNIL値がありました。うまくいけば、これはいくつかの人々をいくつかの時間を節約できます。 – DaveG

+1

こんにちは、それは整理されているので、インポートプロセスは、何人かの作者が 'nil'だったときに終了しましたか?...また、これを再利用することもできます:https://github.com/karmi/railscasts-episodes/commit/ee1f6f39002f32ad25134c81dd6de74ff1b708fa特別なメソッドを作成せずに関連付けを索引付けするためのものです。 – karmi

+0

はい、私の関連の外部キーの一部がnilだったので、インポート処理は機能しませんでした。それはそのようには思わなかった。提案をありがとう、私はそれを見てみましょう。 – DaveG

答えて

2

エラーが発生した理由は、データベースにいくつかありました。うまくいけば、これはいくつかの人々をいくつかの時間を節約できます。