I am trying to create some test data to fill my tables with so that i can test functionality on my site.
The tables in question are: songs, song_arrangements, and song_arrangement_files. The are associated 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 have been able to create 25 songs with FactoryGirl with 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 are correctly assciated with their respective song_arrangement or song record?
I’m guessing i could use after(:create) nested in my factory somehow. I’m very new to FactoryGirl and still fairly new to Rails in general. Any help is much appreciated!
So my other answer helped me do what i needed to do; however, i was needing to basically go one level of associations deeper and i was hitting walls with that solution. I ended re-reading the FactoryGirl documentation for associations and came up with this solution that works in all my cases. It creates songs, song_arrangements, and song_arrangement_files. I’m sure the code isn’t pretty, but it works and can be improved upon later. Hope this helps anyone running into the same type of roadblocks.
Code used to call these factories: