我刚刚进入工厂女孩,我遇到了一个我确信应该容易得多的困难。我只是无法将文档扭曲成一个工作示例。
假设我有以下模型:
class League < ActiveRecord::Base
has_many :teams
end
class Team < ActiveRecord::Base
belongs_to :league
has_many :players
end
class Player < ActiveRecord::Base
belongs_to :team
end
我想做的是:
team = Factory.build(:team_with_players)
并让它为我培养一批玩家。我试过这个:
Factory.define :team_with_players, :class => :team do |t|
t.sequence {|n| "team-#{n}" }
t.players {|p|
25.times {Factory.build(:player, :team => t)}
}
end
但这在该:team=>t
部分失败,因为t
它不是真正的 a Team
,它是 a Factory::Proxy::Builder
。我必须将一支球队分配给一名球员。
在某些情况下,我想建立一个League
并让它做类似的事情,创建多个包含多个玩家的团队。
我错过了什么?