0

我已經在我的模型有多種HAS_ONE協會思考獅身人面像和多個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... 

更改語法從客戶(:tbClientName)customer.tbClientName不是解決方案。

一些幫助,將不勝感激

回答

1

嗯,首先:我不認爲你真的想所有這些列組合成只有一個領域,只有一個屬性?

所以,刪除[] - 你可以傳入多個列,但是如果你傳入一個顯式數組,那麼它將把這些列合併成一個字段/屬性。因此,除去:as選項 - 您不希望所有這些列具有相同的名稱,生成的SQL語句將沒有任何意義。

其次:屬性可按其性質排序,因此您不需要將:sortable => true傳遞給has調用。另外:你真的需要所有的領域是可排序的嗎?最好只標記應該可以排序的字段。

但最後,也許最重要的是:實際發生的錯誤是因爲您的兩個關聯的條件散列。您在同一個表上連接兩次,但在條件中沒有任何表引用。嘗試將兩個條件設置更改爲以下內容:

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

謝謝!你的解決方案完美無缺 – zachar