ruby-on-rails - FactoryGirl:ActiveRecord::AssociationTypeMismatch:用户预期,得到 #<Class:0x007>

标签 ruby-on-rails factory-bot

我有一个带有 has_many User 关联的简单 :photos 模型。当我尝试 FactoryGirl.create(:user) 时,出现以下错误。任何想法?

# user.rb
has_many :photos, dependent: :destroy

# photo.rb
belongs_to :user


FactoryGirl.define do
  factory :user do
    email 'john@site.com'
    after_build do |user|
      user.photos << FactoryGirl.build(:photo, user: user)
    end
  end

  factory :photo do
    photo File.new(File.join(::Rails.root.to_s, "/factories/images", "avatar1.jpg"), 'rb')
  end

错误输出。
ruby-1.9.2-p290 :015 > b = FactoryGirl.create(:user)
ActiveRecord::AssociationTypeMismatch: User(#70183166983920) expected, got #    <Class:0x007fa995612538>(#70183166186140)
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/activerecord-3.2.8/lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/activerecord-3.2.8/lib/active_record/associations/belongs_to_association.rb:6:in `replace'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/activerecord-3.2.8/lib/active_record/associations/singular_association.rb:17:in `writer'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/activerecord-3.2.8/lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/evaluation.rb:12:in `object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/strategy/build.rb:9:in `result'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/factory.rb:42:in `run'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/activesupport-3.2.8/lib/active_support/notifications.rb:125:in `instrument'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:22:in `run'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'
    ... 8 levels...
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/attribute_assigner.rb:14:in `object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/evaluation.rb:12:in `object'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/strategy/create.rb:9:in `result'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/factory.rb:42:in `run'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/activesupport-3.2.8/lib/active_support/notifications.rb:125:in `instrument'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:22:in `run'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/factory_girl-4.1.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method'
    from (irb):15
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /Users/user/.rvm/gems/ruby-1.9.2-p290@site/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'ruby-1.9.2-p290 :016 > #<Class:0x007fa995612538>

最佳答案

解决了。我应该使用更新的 after(:create) 而不是旧的 after_build

关于ruby-on-rails - FactoryGirl:ActiveRecord::AssociationTypeMismatch:用户预期,得到 #<Class:0x007>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13609586/

相关文章:

ruby-on-rails - 如果 rails html.erb 中的每个条件中的任何一个为真,如何仅显示一次 <p>

ruby-on-rails - 使用/不使用 OAuth 和 OpenID 构建 API

ruby-on-rails - 你应该测试 Rails 应用程序使用的 gem 吗?如果是这样,你如何?

ruby-on-rails - Ruby on Rails - 将日期时间的哈希值转换为日期

ruby-on-rails - 带有局部变量的 FactoryGirl

ruby-on-rails-3 - Rails 3 + FactoryGirl : NameError: uninitialized constant Factory

cucumber - Capybara FactoryGirl Carrierwave 无法附加文件

ruby-on-rails - 用于构建新操作的 rspec 匹配器

ruby-on-rails - 为演示者定制工厂女孩 linting

ruby-on-rails - FactoryGirl initialize_with 多个参数