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