2011-04-05 33 views
1

我有一個分支機構和員工之間的多對多關係,並希望使用思維獅身人面像(TS)來搜索current_staff成員擁有的分類帳。思維 - 獅身人面像has_many:通過關係

在Rails 3控制檯我已經試過以下

> Ledger.search 'test', :include => :staff_ids, :with => {:staff_ids => 1} 
Returns [] 
> Ledger.search 'test', :include => :staff_id, :condition => {:staff_id => 1} 
Returns ledgers which do not belong to staff member with id 1. 

萊傑型號

​​

員工型號

class Staff < ActiveRecord::Base 
    has_many :employees 
    has_many :ledgers, :through => :employees 

    define_index do 
    indexes username 
    indexes surname 
    indexes firstname 

    set_property :delta => true # http://freelancing-god.github.com/ts/en/deltas.html  
    end 
end 

我有以下拐點設置

inflect.uncountable %w(staff) 

我試過索引,並在我的總帳模型中,每次更改索引後重建和重新啓動TS。

UPDATE1:

我非常接近。 如果我做了「耙TS:重新索引」,然後重新啓動我的導軌服務器,我的分類帳(關聯到current_staff)搜索工作正常:

@results = Ledger.search params[:search], :include =>:staff_id, :conditions =>{:staff_id => current_staff.id} 

這幾乎就像我必須對我的has_many增量:通過模型,這是正確的嗎?

更新2:

我已經開始與達美航空和協會發揮各地的思維 - 獅身人面像文檔。但似乎仍然沒有工作。

以下是我已經把我的帳型號:

after_save :set_staff_delta_flag 
    ... 
    private 

    def set_staff_delta_flag 
    staff.delta = true 
    staff.save 
    end 

嘗試添加新的總帳時,我得到了以下錯誤:

NoMethodError (undefined method `delta=' for #<Class:0x00000001516340>): 
    app/models/ledger.rb:19:in `set_staff_delta_flag' 
    app/controllers/ledgers_controller.rb:24:in `create' 

回答

2

我找到了答案!

我必須在保存後設置員工增量標誌,但由於我有多個我必須爲每個員工這樣做。

這裏是我的賬模型

class Ledger < ActiveRecord::Base 
    has_many :employees 
    has_many :staff, :through => :employees 

    after_save :set_staff_delta_flag 

    define_index do 
    indexes :name 
    indexes staff.id, :as => :staff_id # many to many relationship. 
    set_property :delta => true 
    end 

    private 

    def set_staff_delta_flag 
    staff.map{ |s| s.update_attribute("delta", true)} 
    end 
end 
在我的Rails應用程序

現在,如果我添加一個新的分類帳的當前工作人員,並使用以下我可以看到新的臺賬在搜索結果中。

@results = Ledger.search params[:search], :include =>:staff_id, :conditions =>{:staff_id => current_staff.id} 

如果有更好的方法做到這一點,讓我知道。

0

當我加入attr_accessor :delta到我是delta索引的模型時,它解決了我自己。

相關問題