0

有没有人遇到过这个:

我有这样的事情:

楷模:

class User < ActiveRecord::Base

    authenticates_with_sorcery!

    attr_accessible :forename, :surname, :email, :password, :password_confirmation
...
end

class Manager < User
  has_many :things_to_manage

...
end

class ThingsToManage < ActiveRecord::Base
  attr_accessible :stuff, :more_stuff

  belongs_to :manager

...
end

我想建立一个工厂,以便 FactoryGirl.create(:things_to_manage) 将创建一个带有管理器(继承自用户)的 ThingsToManager 模型(保存在数据库中)

我的工厂是这样的:

FactoryGirl.define do
    factory :user do
        forename 'Test'
        surname 'User'
        sequence(:email) {|n| "#{forename}.#{surname}#{n}@test.com".downcase}
        password 'lalala123@@'
        end
end

FactoryGirl.define do
  factory :manager do

  end
end

FactoryGirl.define do
  factory :things_to_manage do

    stuff "rhubarb rhubarb"
    more_stuff "la la la"
    association :manager, :factory => :user

  end
end

association :manager, :factory => :user不起作用;我明白了

ActiveRecord::AssociationTypeMismatch: Manager(#70275174714940) 预期,得到用户(#70275151319360)

有任何想法吗?

4

1 回答 1

0

根据这个答案,您可以尝试将您的经理工厂更改为:

FactoryGirl.define do
  factory :manager, class: Manager, :parent => :user do

  end
end

然后你的 things_to_manage 工厂:

FactoryGirl.define do
  factory :things_to_manage do

    stuff "rhubarb rhubarb"
    more_stuff "la la la"
    manager

  end
end
于 2013-10-18T11:24:50.163 回答