Note that there are some explanatory texts on larger screens.

plurals
  1. POthinking-sphinx has_many :through relationships
    primarykey
    data
    text
    <p>I've got a many-to-many relationship between ledger and staff and would like to use Thinking-Sphinx (TS) to search ledgers which the current_staff member owns.</p> <p>In Rails 3 console I've tried the following</p> <pre><code>&gt; Ledger.search 'test', :include =&gt; :staff_ids, :with =&gt; {:staff_ids =&gt; 1} Returns [] &gt; Ledger.search 'test', :include =&gt; :staff_id, :condition =&gt; {:staff_id =&gt; 1} Returns ledgers which do not belong to staff member with id 1. </code></pre> <p>Ledger Model</p> <pre><code>class Ledger &lt; ActiveRecord::Base has_many :employees has_many :staff, :through =&gt; :employees define_index do indexes :name # indexes staff.id, :as =&gt; :staff_id # many to many relationship. has staff(:id), :as =&gt; :staff_ids # many to many relationship. set_property :delta =&gt; true end end </code></pre> <p>Staff Model</p> <pre><code>class Staff &lt; ActiveRecord::Base has_many :employees has_many :ledgers, :through =&gt; :employees define_index do indexes username indexes surname indexes firstname set_property :delta =&gt; true # http://freelancing-god.github.com/ts/en/deltas.html end end </code></pre> <p>I have got the following inflection setup</p> <pre><code> inflect.uncountable %w( staff ) </code></pre> <p>I've tried indexes and has in my Ledger model, rebuilding and restarting TS after each change to indexes.</p> <p><strong>Update1:</strong> </p> <p>I'm very close. If I do a 'rake ts:reindex' then restart me rails server, my search on ledgers (associated to the current_staff) works fine:</p> <pre><code>@results = Ledger.search params[:search], :include =&gt;:staff_id, :conditions =&gt;{:staff_id =&gt; current_staff.id} </code></pre> <p>It's almost like I have to have a delta on my has_many :through model, is this correct?</p> <p><strong>Update 2:</strong></p> <p>I've started to play around with Delta's and associations as in the thinking-sphinx documentation. Still doesn't seem to work though.</p> <p>Here is what I've put in my Ledger model:</p> <pre><code> after_save :set_staff_delta_flag ... private def set_staff_delta_flag staff.delta = true staff.save end </code></pre> <p>I get the following error when trying to add a new ledger:</p> <pre><code>NoMethodError (undefined method `delta=' for #&lt;Class:0x00000001516340&gt;): app/models/ledger.rb:19:in `set_staff_delta_flag' app/controllers/ledgers_controller.rb:24:in `create' </code></pre>
    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.
    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