开发者

rails3 - uninitialized constant in factory definition

factory_girl 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.

Factory definition:

Factory.define :interest, :class => Term::Interest do |f|
  f.name {"#{Factory.next(:lipsum_word)}ing"}
end

Definition of Term and Term::Interest

class Ter开发者_运维技巧m < ActiveRecord::Base
  belongs_to :category

  class Interest < Term
  end

  class Award < Term
  end  
end

Error and stack trace:

$ 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 `<top (required)>'
/Users/glurban/code/recruitd/lib/tasks/use_factories.rake:2:in `<top (required)>'
/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 `<top (required)>'
/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 `<top (required)>'
/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 `<main>'

I tried adding require_dependency 'term' to the top of factories.rb but then I get

Glens-MacBook-Pro:test glurban$ rake db:data:load
(in /Users/glurban/code/recruitd)
rake aborted!
No such file to load -- term

What to do?

Edit: in response to the comment, yes, it happens only on rake, not in the console:

$ rails c
Loading development environment (Rails 3.0.0)
ruby-1.9.2-rc2 > Factory(:term)
 => #<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"> 
ruby-1.9.2-rc2 > Factory(:interest)
 => #<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"> 

Custom rake task definition:

require 'factory_girl'
require File.expand_path("test/factories/factories.rb")

namespace :db do
  namespace :data do 

    desc "Load sample data"
    task :load => :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 => input.strip)
  end
  puts "Created interests"
end


After looking over the factory_girl documentation again, I realized that you can specify a class using either a string or a class constant. So I tried using a string and it worked:

Factory.define :interest, :class => "Term::Interest" do |f|
  f.name {"#{Factory.next(:lipsum_word)}ing"}
end

Also, a comment about the nested classes. I initially did that to keep the classes a little more organized, but given the complexity it's created (especially for routing), I'm just going to move each subclass to its own model file. (If I were to un-nest them without putting them in separate files, rails seems to fail to find the subclasses sometimes--namely if the parent class hadn't been referenced (forcing the load of that file) before.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜