title: rails3 - uninitialized constant in factory definition
body: <p><code>factory_girl</code> is not recognizing a model name that I've defined, and which I need to reference because I need a subclass.  This worked in rails 2 but I'm migrating to rails 3.</p>
  
  <p>Factory definition:</p>
  
  <pre><code>Factory.define :interest, :class =&gt; Term::Interest do |f|
    f.name {"#{Factory.next(:lipsum_word)}ing"}
  end
  </code></pre>
  
  <p>Definition of <code>Term</code> and <code>Term::Interest</code></p>
  
  <pre><code>class Term &lt; ActiveRecord::Base
    belongs_to :category
  
    class Interest &lt; Term
    end
  
    class Award &lt; Term
    end  
  end
  </code></pre>
  
  <p>Error and stack trace:</p>
  
  <pre><code>$ rake db:data:load --trace
  (in /Users/glurban/code/recruitd)
  rake aborted!
  uninitialized constant Term
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rspec-core-2.4.0/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
  /Users/glurban/code/recruitd/test/factories/factories.rb:316:in `&lt;top (required)&gt;'
  /Users/glurban/code/recruitd/lib/tasks/use_factories.rake:2:in `&lt;top (required)&gt;'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/engine.rb:131:in `block in load_tasks'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/engine.rb:131:in `each'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/engine.rb:131:in `load_tasks'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/application.rb:141:in `load_tasks'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0/lib/rails/application.rb:77:in `method_missing'
  /Users/glurban/code/recruitd/Rakefile:7:in `&lt;top (required)&gt;'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2383:in `raw_load_rakefile'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2017:in `block in load_rakefile'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2000:in `block in run'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/gems/rake-0.8.7/bin/rake:31:in `&lt;top (required)&gt;'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/bin/rake:19:in `load'
  /Users/glurban/.rvm/gems/ruby-1.9.2-rc2/bin/rake:19:in `&lt;main&gt;'
  </code></pre>
  
  <p>I tried adding <code>require_dependency 'term'</code> to the top of <code>factories.rb</code> but then I get</p>
  
  <pre><code>Glens-MacBook-Pro:test glurban$ rake db:data:load
  (in /Users/glurban/code/recruitd)
  rake aborted!
  No such file to load -- term
  </code></pre>
  
  <p>What to do?</p>
  
  <p>Edit: in response to the comment, yes, it happens only on rake, not in the console:</p>
  
  <pre><code>$ rails c
  Loading development environment (Rails 3.0.0)
  ruby-1.9.2-rc2 &gt; Factory(:term)
   =&gt; #&lt;Term id: 3, type: nil, name: "Proud to be a Recruitd user", location: nil, category_id: nil, description: nil, url: nil, created_at: "2011-01-06 21:30:14", updated_at: "2011-01-06 21:30:14"&gt; 
  ruby-1.9.2-rc2 &gt; Factory(:interest)
   =&gt; #&lt;Term::Interest id: 4, type: "Term::Interest", name: "siting", location: nil, category_id: nil, description: nil, url: nil, created_at: "2011-01-06 21:30:18", updated_at: "2011-01-06 21:30:18"&gt; 
  </code></pre>
  
  <p>Custom rake task definition:</p>
  
  <pre><code>require 'factory_girl'
  require File.expand_path("test/factories/factories.rb")
  
  namespace :db do
    namespace :data do 
  
      desc "Load sample data"
      task :load =&gt; :environment do |t|
        create_students
        ...
        create_student_files_and_feeds
        puts "Completed loading sample data."
      end
    end
  end
  
  def create_interests
    data_fetch("interests").each do |input|
      Factory(:interest, :name =&gt; input.strip)
    end
    puts "Created interests"
  end
  </code></pre>