我如何从rails初始化一个gem而不必实例化类



我在rails控制台发出这些命令,我想知道为什么我需要实例化文章。新的为了rails加载我的"livemattri -models"宝石?

1.9.3p286 :011 > defined?(Article)
 => nil
1.9.3p286 :012 > require 'livemattr-models'
 => false
1.9.3p286 :013 > defined?(Article)
 => nil
1.9.3p286 :014 > Article.new
 => #<Article _id: 51b1d5c20be168263b000001>
1.9.3p286 :015 > defined?(Article)
 => "constant"

p。我正试图解决这个问题,因为我的rake一直爆炸,因为我的类还没有加载。

当您在开发中运行控制台时,Rails不会在启动时加载所有类,而是在需要时动态加载它们。

因此,当你实例化一个Article时,它将加载这个类。

如果您想删除此行为,请将其添加到您的环境/development.rb

 config.cache_classes = true

但是它会阻止rails自动重新加载你的类,并且可能会在启动时慢得多!

你可能还想了解更多关于cache_classes: http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html

相关内容

最新更新