2017-01-13 52 views
0

我有一个模型类叫Person。 人员类别具有名为car的属性。添加关注模块到模型

现在我想在一个名为Person::Car::HasProducer

关注模型来定义模块的模块应该看起来像:

module HasProducer 

    def produced_by_toyota? 
    car == "Prius" 
    end 

    def produced_by_bmw? 
    car == "X3" || car == "X5" 
    end 
end 

我想找到下这个文件:顾虑/人/汽车/ has_producer.rb

Person -class我试图把它列入这样的:

class Person 
    include Person::Car::HasProducer 

但我得到的错误:Unable to autoload constant Person::Car::HasProducer

我试过的modules/classes不同的组合,但没有一个星座为我工作。

我该怎么做has_producer.rb得到的结果是Person::Car::HasProducer

回答

1

无论是定义你的模块相匹配的命名空间Person::Car::HasProducer即类似以下内容:

class Person 
    module Car 
    module HashProducer 
     # your code here 
    end 
    end 
end 

或只是include HasProducer

+0

感谢您的评论,它的工作通过改变第一'module'为'class' 。太好了,你也注意到了'ActiveSupport :: Concern'。在这个示例代码中我省略了它。请更新您的问题,我会将其标记为正确! –

+0

将第一个'module'更新为'class' –