我已经看过一些旧语法的例子,但我找不到使用新的 3.x 语法的例子(一个这样的旧例子:factory girl multiple has_many through's)。
楷模
class RawPosition < ActiveRecord::Base
has_many :position_translations
has_many :specific_positions, through: :position_translations
end
class SpecificPosition < ActiveRecord::Base
has_many :position_translations
has_many :raw_positions, through: :position_translations
end
class PositionTranslation < ActiveRecord::Base
belongs_to :raw_position
belongs_to :specific_position
end
工厂
factory :raw_poisition_multiple, class: RawPosition do
raw_input "WR/QB"
sport_type_id 5
after_create do |a|
#a.specific_positions.create({specific_position: "WR"})
#a.specific_positions.create({specific_position: "QB"})
FactoryGirl.create(:specific_position, raw_position: a)
FactoryGirl.create(:qb_specific_position, raw_position: a)
end
end
factory :specific_position do
specific_position "WR"
end
factory :qb_specific_position do
specific_position "QB"
end
规格
describe "WR/QB" do
before do
@player.player_dict['POS'] = "WR/QB"
FactoryGirl.create(:raw_poisition_multiple)
@player.clean_position(@player_to_team_history)
end
....
end
如果我取消注释raw_position_multiple
工厂中的行并注释掉块FactoryGirl
中的行after_create
,一切正常。我只是想要使用工厂来创建关联的能力。