【问题标题】:FactoryGirl: ActiveRecord::AssociationTypeMismatch: User expected, got #<Class:0x007>FactoryGirl:ActiveRecord::AssociationTypeMismatch:用户预期,得到#<Class:0x007>
【发布时间】:2012-11-28 16:18:25
【问题描述】:

我有一个简单的 User 模型,带有一个 has_many :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>

【问题讨论】:

  • 这个工作:user.photos
  • 当前版本使用不同的格式定义回调,也许你应该在build之后使用而不是create之后使用
  • 已解决。我应该使用较新的 after(:create) 而不是较旧的 after_build
  • @Martin 如果您发现错误,请回答您自己的问题。

标签: ruby-on-rails factory-bot


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,这是我用来解决它的代码块:

    after(:create) do |user|
      user.photos << FactoryGirl.create :photo, user: user
    end
    

    【讨论】:

      【解决方案3】:

      我在验证 has_many 关联是否至少有一个关联模型时遇到了类似问题:

          class Classroom < ActiveRecord::Base
            # Relationships
            belongs_to :course
            has_many :weekdays
      
            # Validations
            validate :has_weekdays
      
            def has_weekdays
              errors.add(:base, 'it is invalid without at least one weekday') if   self.weekdays.blank?
            end
      
          end
      

      所以,为了使工厂有效,这是我的代码

      FactoryGirl.define do
        factory :classroom do
          course    
      
          after (:build) do |classroom|
            classroom.weekdays << FactoryGirl.create(:weekdays, classroom: classroom)
          end 
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多