2012-12-31 52 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. 
相关问题