2012-03-12 167 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 

用戶出於某種原因,在視圖上的輪胎進口進行耙或偶爾,當我不斷收到以下錯誤/錯誤

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 2012-03-12 21:37:37

+1

嗨,很好它是整理出來的 - 所以導入過程死了,當一些作者是'無'?...此外,也許你可以重複使用:https://github.com/karmi/railscasts-episodes/commit/ee1f6f39002f32ad25134c81dd6de74ff1b708fa爲索引關聯而不創建特殊方法... – karmi 2012-03-13 08:24:27

+0

是的,導入過程不起作用,因爲我的一些關聯外鍵是零。它似乎不喜歡那樣。感謝您的建議,我會看看它。 – DaveG 2012-03-13 16:55:50

回答

2

我在我的數據庫中有幾個NIL值是導致錯誤的原因。希望這可以節省一些人一些時間。