2012-12-31 49 views
1

我有一個名字間隔模式叫Accounting::Invoice如何在Rails中使用has_many through命名空間模型?

我也有一個叫做模型Contact

Accounting::Invoice有通過所謂Contactable另一個模型多次接觸 - 這是在發票是在一個以上的分裂情況聯繫。

但由於某種原因,我不能讓has_many through協會工作。

我可以得到它的一個方向,前工作:Contact.first.accounting_invoices,但走另一條路,例如:Accounting::Invoices.first.contacts讓我看起來像一個錯誤:

SQLite3::SQLException: no such column: contactables.invoice_id...

任何想法?我不確定ActiveRecord在哪裏獲得contactabes.invoice_id。我已明確定義了我的表名和類名,以分別使用accounting_invoices表和Accounting::Invoice類。

+0

請告訴你如何在這兩種模式成立協會,以及任何其他可能相關的模型設置(如自定義'table_name')。 –

回答

1

您可以使用:foreign_key => 'accounting_invoice_id選項來告訴導軌使用哪個字段。查看has_many的文檔瞭解更多信息(http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many)。

相關部分是:

:foreign_key 
Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and 「_id」 suffixed. So a Person class that makes a has_many association will use 「person_id」 as the default :foreign_key. 
相關問題