我有一个产品型号:
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, a save消息应该发送到产品,因此在这种情况下,我应该针对与子类别相关的每个产品并发送save消息,类似这样的:
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* 发送到 Product 模型。
修复此问题后,一切正常,但 delta_index 未正确更新,我需要做save两次到子类别模型。
所以我的最终解决方案是这样的:
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
Using 提交后 and 定义索引是正确的解决方案吗?这是我找到的唯一一个。