Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Delta Indexes for associations in Thinking Sphinx
    primarykey
    data
    text
    <p>I have a Product model:</p> <pre><code>class Product &lt; ActiveRecord::Base belongs_to :subcategory define_index do # fields indexes subcategory.name, :as =&gt; :subcategory, :sortable =&gt; true, :facet =&gt; true # attributes has subcategory_id, created_at, updated_at #properties set_property :delta =&gt; true </code></pre> <p>Now, suppose that a user updates a subcategory name, which is the proper way to update the products delta index?</p> <p>According to this documentation: <a href="http://freelancing-god.github.com/ts/en/deltas.html" rel="nofollow">http://freelancing-god.github.com/ts/en/deltas.html</a>, a <em>save</em> message should be sent to the product, so in this case I should go for each product related with the subcategory and send the <em>save</em> message, something like this:</p> <pre><code>class Subcategory &lt; 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 </code></pre> <p>I think that this is overkilling because we have like 100.000 products per subcategory. Is this the correct way to update the delta index? Am I missing something?</p> <p>After adding this:</p> <pre><code>def set_product_delta_flag Product.update_all ['delta = ?', true], ['subcategory_id = ?', id] Product.index_delta end </code></pre> <p>I'm always receiving this error:</p> <p>NoMethodError (undefined method `index_delta' for #):</p> <p>So, the solution to this problem was to send the message *define_indexes* to the Product model. </p> <p>After fixing this issue, everything was ok, but the delta_index was not correctly updated, I needed to do <em>save</em> twice to the subcategory model.</p> <p>So my final solution is this one:</p> <pre><code>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 </code></pre> <p>Using <strong>after_commit</strong> and <strong>define_indexes</strong> is the correct solution? Its the only one that I've found.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload