2017-02-16 70 views
0

我想命名空间“甜点”下的一些“水果”模型,所以我创建了一个名为“甜点”的模型子目录,并在那里放置了“水果”模型。如何从相关模型引用名称空间模型?

应用程序/模型/点心/ fruit.rb

class Dessert::Fruit < ActiveRecord::Base 
    def self.table_name_prefix 
     'dessert_' 
    end 
end 

所附的表称为:dessert_fruits,我能够进入轨道控制台,并成功执行Dessert::Fruit.all

现在我想创建使用has_oneaccepts_nested_attributes_for另一个模型(meal.rb)的关联,但我不知道如何来引用命名空间模型(xxxxx下图):

应用程序/模型/ meal.rb

class Meal < ActiveRecord::Base 
    has_one :xxxxx, dependent: :destroy, autosave: true 
    accepts_nested_attributes_for :xxxxx 
    # replacing :xxxxx with :dessert_fruit does not work 
end 

回答

1

尝试添加类的名称明确:

class Meal < ActiveRecord::Base 
    has_one :fruit, dependent: :destroy, autosave: true, class_name: '::Dessert::Fruit' 
    accepts_nested_attributes_for :fruit 
end 

This article对组织模块有更深入的讨论。

+0

非常感谢。你的回答让我继续我的工作。我很感激! – user664833

相关问题