Rails 3 - FactoryGirl creates related records

I am trying to create some test data to populate my tables so that I can test the functionality on my site.

Corresponding tables: songs , song_arrangements and song_arrangement_files . They are connected in this way.

 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 

I managed to create 25 songs with FactoryGirl using this code:

 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 

Any thoughts on how to create song_arrangements and song_arrangement_files that were correctly indexed with their corresponding song_arrangement or song record?

I assume that I could use after(:create) somehow nested in my factory. I am very new to FactoryGirl and still pretty new to Rails in general. Any help is much appreciated!

+4
source share
2 answers

So my other answer helped me do what I needed to do; however, I needed to basically go one level of connections deeper, and I hit the walls with this decision. I ended up rereading the FactoryGirl documentation for associations and came up with this solution that works in all my cases. He creates songs, song_arrangements and song_arrangement_files. I'm sure the code is not very good, but it works and can be improved later. Hope this helps anyone who works on the same type of roadblock.

 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 

The code used to call these plants:

 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) } 
+3
source

For everyone who works on this issue, here is the solution I came across. As I said, I'm new to FactoryGirl, so if there is a better way to do this, please share!

 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 

By providing credit where credit should be, I really found the answer after much searching from this post: As a factory girl user to create linked lists with has_many with the confirmation that is required to create .

This is Blizzo's answer to which I pulled out a solution.

+2
source

All Articles