Kamilski81
Kamilski81

Reputation: 15107

How do i initialize a gem from rails without having to instantiate the class?

I am issuing these commands in rails console and am wondering why i need to instantiate Article.new in order for rails to load my '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"

ps. I am trying to resolve this because my rake keeps bombing out cause my classes have not been loaded.

Upvotes: 0

Views: 145

Answers (1)

Arkan
Arkan

Reputation: 6236

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

Upvotes: 1

Related Questions