思考Sphinx和多个has_one

时间:2011-11-04 11:44:21

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-plugins thinking-sphinx

我的模型中有多个has_one关联

class Invoice < ActiveRecord::Base
...
belongs_to :seller, :class_name => "Client", :foreign_key => "provider", :conditions => ['is_provider = ?', true]
belongs_to :customer, :class_name => "Client", :foreign_key => "receiver", :conditions => ['is_receiver = ?', true]

我正在尝试索引它:

indexes [seller(:tbClientCode), seller(:tbClientLabel), seller(:tbClientName), seller(:tbClientNIP), 
  seller(:tbClientRegon), seller(:tbClientZip), seller(:tbClientCity), seller(:tbClientStreet), 
  seller(:tbClientHouseNr), seller(:tbClientHomeNr], :sortable => true, :as => :seller_fields    
has  [seller(:is_provider), seller(:is_receiver)], :sortable => true, :as => :seller_attributes      

indexes [customer(:tbClientCode), customer(:tbClientLabel), customer(:tbClientName), customer(:tbClientNIP), 
  customer(:tbClientRegon), customer(:tbClientZip), customer(:tbClientCity), customer(:tbClientStreet), 
  customer(:tbClientHouseNr), customer(:tbClientHomeNr], :sortable => true), :as => :customer_fields    
has  [customer(:is_provider), customer(:is_receiver)], :sortable => true, :as => :customer_attributes

...然后发生一些错误

ERROR: index 'invoice_core': sql_range_query: ERROR:  column reference "is_receiver" is ambiguous
LINE 1: ...tomers_invoices"."id" = "invoices"."receiver" AND is_receive...

customer(:tbClientName)的语法更改为 customer.tbClientName 不是解决方案。

非常感谢一些帮助

1 个答案:

答案 0 :(得分:1)

嗯,首先:我认为你真的不想将所有这些列合并为一个字段而只是一个属性?

因此,删除[] - 您可以传入多个列,但如果传入显式数组,则会将这些列组合到一个字段/属性中。因此,同样删除:as选项 - 您不希望所有这些列具有相同的名称,生成的SQL语句将没有任何意义。

其次:属性可以按其性质进行排序,因此您无需将:sortable => true传递给has来电。另外:你真的需要所有的字段都可以排序吗?最好只标记应该可以排序的字段。

但最后,也许最重要的是:实际发生的错误是因为你的两个关联的条件哈希。您正在两次加入同一个表,但在条件中没有任何表引用。尝试将两个条件设置更改为以下内容:

:conditions => {:is_provider => true}
:conditions => {:is_receiver => true}