2011-12-01 66 views
2
class Contact < ActiveRecord::Base 
    has_many :records 
end 

class Volunteering::Record < ActiveRecord::Base 
    belongs_to :contact 
end 

rails console 
    > Contact.last.records #NameError: uninitialized constant Contact::Record 

如果我改变类联系到的has_many:volunteering_records然后我得到:Rails的一个一对多的关系,方法

Contact.last.volunteering_records # NameError: uninitialized constant Contact::VolunteeringRecord

我的志愿::记录类有外键:

Volunteering::Record.column_names.include?('contact_id') # true

有什么建议吗?

更新: 我想通了。因为它是在不同的命名空间,完整的类名,需要指定:

:has_many records, :class_name => "Volunteering::Record" 
+0

为什么'Voluntering:Record'?为什么不用'Record'或'VolunteringRecord'? –

+2

自从我在Rails中使用名称空间模型以来,已经有一段时间了。我只能说,除非你有说服力,否则需要命名空间模型,避免它像瘟疫!只要使用Record Record

回答

0

更改志愿::记录只是记录和你的例子应该只是罚款。如果你正在寻找一个中间表,你想使用:has_many,:through。