2
我有一個產品型號:使用增量指標的協會思考獅身人面像
class Product < ActiveRecord::Base
belongs_to :subcategory
define_index do
# fields
indexes subcategory.name, :as => :subcategory, :sortable => true, :facet => true
# attributes
has subcategory_id, created_at, updated_at
#properties
set_property :delta => true
現在,假設一個用戶更新了子類別名稱,這是更新的產品增量索引的正確方法?
根據此文件:http://freelancing-god.github.com/ts/en/deltas.html,一個保存消息應該被髮送到該產品,所以在這種情況下,我應該去與子類相關的每一件產品,併發送保存消息,是這樣的:
class Subcategory < ActiveRecord::Base
has_many :products
after_save :set_product_delta_flag
private
def set_product_delta_flag
products.each { |product|
product.delta = true
product.save
}
end
end
我認爲這太過分了,因爲我們每個子類別都有100.000個產品。 這是更新增量索引的正確方法嗎?我錯過了什麼嗎?
添加此之後:
def set_product_delta_flag
Product.update_all ['delta = ?', true], ['subcategory_id = ?', id]
Product.index_delta
end
我總是收到此錯誤:
NoMethodError(未定義的方法`index_delta」爲#):
因此,解決這個問題是將消息* define_indexes *發送到產品模型。
修復此問題後,一切正常,但delta_index沒有正確更新,我需要做保存兩倍的子類別模型。
所以我的最終解決方案,這是一個:
after_commit :set_product_delta_flag
private
def set_product_delta_flag
Product.define_indexes
Product.update_all ['delta = ?', true], ['subcategory_id = ?', id]
Product.index_delta
end
使用after_commit和define_indexes是正確的解決方案?它是我找到的唯一一個。
真的很酷,非常感謝。 – 2011-02-05 15:18:04