0

我在 rails 控制台中发出这些命令,我​​想知道为什么我需要实例化 Article.new 以让 rails 加载我的“livemattr-models”gem?

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"

附言。我正在尝试解决这个问题,因为我的 rake 一直在轰炸,因为我的课程没有被加载。

4

1 回答 1

1

When you're running your console in development, Rails do not load all the classes at startup, but it will instead load them on the fly when you need them.

Thus, when you instantiate an Article, it will load the classe.

If you want to remove this behavior, add this to your environment/development.rb

 config.cache_classes = true

But it will prevent rails to auto reload your classes, and might be way slower at startup !

You also maybe want to learn more about cache_classes: http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html

于 2013-06-07T13:18:51.703 回答