Thinking-sphinx has_many :通过关系

发布于 2024-10-29 20:40:42 字数 2052 浏览 1 评论 0原文

我在分类帐和员工之间建立了多对多关系,并且想使用 Thinking-Sphinx (TS) 来搜索 current_staff 成员拥有的分类帐。

在 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.

Ledger 模型

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

  define_index do
    indexes :name
#    indexes staff.id, :as => :staff_id # many to many relationship.
    has staff(:id), :as => :staff_ids # many to many relationship.    

    set_property :delta => true
  end
end

Staff Model

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

我已经尝试了索引并在我的 Ledger 模型中进行了以下变形设置

  inflect.uncountable %w( staff )

,在每次更改索引后重建并重新启动 TS。

更新1:

我非常接近了。 如果我执行“rake ts:reindex”,然后重新启动 Rails 服务器,我对分类帐(与 current_staff 关联)的搜索工作正常:

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

这几乎就像我必须在 has_many :through 模型上有一个增量,这是正确的吗?

更新2:

我已经开始尝试使用Thinking-sphinx文档中的Delta和关联。但似乎仍然不起作用。

这是我在分类帐模型中放入的内容:

  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'

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.

In Rails 3 console I've tried the following

> 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.

Ledger Model

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

  define_index do
    indexes :name
#    indexes staff.id, :as => :staff_id # many to many relationship.
    has staff(:id), :as => :staff_ids # many to many relationship.    

    set_property :delta => true
  end
end

Staff Model

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

I have got the following inflection setup

  inflect.uncountable %w( staff )

I've tried indexes and has in my Ledger model, rebuilding and restarting TS after each change to indexes.

Update1:

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:

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

It's almost like I have to have a delta on my has_many :through model, is this correct?

Update 2:

I've started to play around with Delta's and associations as in the thinking-sphinx documentation. Still doesn't seem to work though.

Here is what I've put in my Ledger model:

  after_save :set_staff_delta_flag
  ...
  private

  def set_staff_delta_flag
    staff.delta = true
    staff.save
  end

I get the following error when trying to add a new ledger:

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-11-05 20:40:42

我找到了答案!

我必须在保存后设置人员增量标志,但由于我有多个人员,所以我必须为每个人员执行此操作。

这是我的分类账模型

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}

如果有更好的方法来做到这一点,请告诉我。

I found the answer!

I have to set the staff delta flag after the save, but as I have multiple I have to do that for each staff.

Here is my ledger model

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

Now in my Rails app if I add a new ledger for a current staff member and use the following I can see the new ledger in the search results.

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

If there is a better way to do this let me know.

欲拥i 2024-11-05 20:40:42

当我将 attr_accessor :delta 添加到我进行增量索引的模型时,它自行解决了。

It resolved itself for me when I added attr_accessor :delta to the models that I was delta indexing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文