4

我正在尝试创建一些测试数据来填充我的表格,以便我可以在我的网站上测试功能。

有问题的表是:songssong_arrangementssong_arrangement_files。以这种方式关联。

Songs:
    has_many :song_arrangements, :dependent => :destroy
    has_many :song_arrangement_files, :through => :song_arrangements

Song Arrangements:
    belongs_to :song
    has_many :song_arrangement_files, :dependent => :destroy

Song Arrangement Files:
    belongs_to :song
    belongs_to :song_arrangement

我已经能够使用 FactoryGirl 使用以下代码创建 25 首歌曲:

Code in my spec file:
    before { FactoryGirl.create_list(:song, 25) }

Code in the factory:
    FactoryGirl.define do
        factory :song do |s|
            s.sequence(:title) { |n| "Song Title #{n}" }
            s.sequence(:artist) { |n| "Song Artist #{n}" }
        end
    end

关于如何创建song_arrangements以及song_arrangement_files与它们各自song_arrangementsong记录正确关联的任何想法?

我猜我可以after(:create)以某种方式在我的工厂中使用嵌套。我对 FactoryGirl 很陌生,而且对 Rails 总体来说还是很陌生。任何帮助深表感谢!

4

2 回答 2

3

所以我的另一个答案帮助我完成了我需要做的事情;但是,我基本上需要更深入的关联,并且我正在用那个解决方案碰壁。我结束了重新阅读有关关联的 FactoryGirl 文档,并提出了适用于我所有情况的解决方案。它创建歌曲、song_arrangements 和 song_arrangement_files。我确信代码不是很漂亮,但它可以工作并且可以在以后改进。希望这可以帮助遇到相同类型障碍的任何人。

FactoryGirl.define do
  factory :song do |s|
    s.sequence(:title) { |n| "Song Title #{n}" }
    s.sequence(:artist) { |n| "Song Artist #{n}" }

    factory :song_with_song_arrangements do
      ignore do
        song_arrangements_count 100
      end

      after(:create) do |song, evaluator|
        FactoryGirl.create_list(:song_arrangement, evaluator.song_arrangements_count, song: song)
      end
    end
  end

  factory :song_arrangement do |sa|
    song

    sa.sequence(:title) { |n| "Arrangement #{n}" }
    original_key 'A'
    bpm 75
    sa.sequence(:chart_content) { |n| "This is the chart content for Arrangement #{n}." }
    chart_mapping 'V1, C, V2, C, B, C, C'
    sa.sequence(:notes) { |n| "These are notes for the Arrangement #{n}." }

    factory :song_arrangement_with_song_arrangement_files do
      ignore do
        song_arrangement_files_count 100
      end

      after(:create) do |song_arrangement, evaluator|
        FactoryGirl.create_list(:song_arrangement_file, evaluator.song_arrangement_files_count, song_arrangement: song_arrangement)
      end
    end
  end

  factory :song_arrangement_file do |saf|
    song_arrangement
    song

    saf.sequence(:title) { |n| "Attachment #{n}" }
    url 'http://www.google.com'
    saf.sequence(:description) { |n| "This is the description of Attachment #{n}." }
  end
end

用于调用这些工厂的代码:

Songs:
    before(:each) { FactoryGirl.create_list(:song, 25) }
Song Arrangements:
    before(:each) { FactoryGirl.create(:song_with_song_arrangements) }
Song Arrangement Files:
    before(:each) { FactoryGirl.create(:song_arrangement_with_song_arrangement_files) }
于 2013-01-10T16:16:59.833 回答
2

对于遇到此问题的其他任何人,这是我提出的解决方案。正如我所说,我是 FactoryGirl 的新手,所以如果有更好的方法,请分享!

FactoryGirl.define do
  factory :song do |s|
    s.sequence(:title) { |n| "Song Title #{n}" }
    s.sequence(:artist) { |n| "Song Artist #{n}" }

    before(:create) do |song|
      song.song_arrangements << FactoryGirl.build_list(:song_arrangement, 10)
    end
  end

  factory :song_arrangement do |sa|
    sa.sequence(:title) { |n| "Arrangement #{n}" }
    original_key 'A'
    bpm 75
    sa.sequence(:chart_content) { |n| "This is the chart content for Arrangement #{n}." }
    chart_mapping 'V1, C, V2, C, B, C, C'
    sa.sequence(:notes) { |n| "These are notes for the Arrangement #{n}." }
  end
end

在信用到期时给予信用,我实际上在从这篇文章中进行了大量搜索后找到了答案:如何使用工厂女孩创建带有 has_many 的关联列表,并在创建时进行验证

我从中提取了解决方案是 Blizzo 的回答。

于 2013-01-08T22:25:00.433 回答