Ruby on rails 创建使用当前种子记录的FactoryGirl定义

Ruby on rails 创建使用当前种子记录的FactoryGirl定义,ruby-on-rails,ruby,factory-bot,Ruby On Rails,Ruby,Factory Bot,我遇到了与FactoryGirl的GitHub页面()相同的问题,唯一性验证被种子数据绊倒 我的工厂原来是这样的: factory :country, class: MyApp::Models::Country do code { MyApp::Models::Country.first.code } name { MyApp::Models::Country.first.name } end 不理想,弄坏了门楣,因此我使用#623中的建议进行了重构: 但是,我现在得到了以下关于lin

我遇到了与FactoryGirl的GitHub页面()相同的问题,唯一性验证被种子数据绊倒

我的工厂原来是这样的:

factory :country, class: MyApp::Models::Country do
  code { MyApp::Models::Country.first.code }
  name { MyApp::Models::Country.first.name }
end
不理想,弄坏了门楣,因此我使用#623中的建议进行了重构:

但是,我现在得到了以下关于linting的错误堆栈:

/Users/philostler/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:435:in `method_missing': undefined method `intitalize_with=' for #<MyApp::Models::Country id: nil, code: nil, name: nil> (NoMethodError)
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:208:in `method_missing'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
/Users/philostler/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.4/lib/active\u model/attribute\u methods.rb:435:in'method_missing':未定义的方法'initalize\u with='for#(命名错误)
from/Users/philostler/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active\u record/attribute\u methods.rb:208:in'method\u missing'
from/Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:16:in `对象中的块(2层)'
from/Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in'each'
from/Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:在“对象中的块”中
from/Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in'tap'
from/Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in'object'
它现在正试图在一个空白的模型对象上调用initalize_with=,当然这个对象会掉下来

我只是希望工厂使用一个已经存在的记录,同时仍然通过皮棉


我这样做是错误的还是有错误(可能是因为我使用的是名称空间)?

在使用FactoryGirl并使用常量声明
类时,我经常遇到不相关的奇怪错误。使用字符串修复了我这边的情况,这当然与自动加载相关

我不知道这是否是你的解决方案,但你可以尝试(对你所有的工厂都这样做)


恐怕没有效果,但这在将来很容易知道。谢谢
/Users/philostler/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:435:in `method_missing': undefined method `intitalize_with=' for #<MyApp::Models::Country id: nil, code: nil, name: nil> (NoMethodError)
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:208:in `method_missing'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
# Instead of
factory :country, class: MyApp::Models::Country do
  code { MyApp::Models::Country.first.code }
  ...
end

# Do
factory :country, class: 'MyApp::Models::Country' do
  code { MyApp::Models::Country.first.code }
  ...
end