Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9112297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:42:09+00:00 2026-06-17T03:42:09+00:00

I am trying to create some test data to fill my tables with so

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:42:11+00:00Added an answer on June 17, 2026 at 3:42 am

    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.

    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
    

    Code used to call these factories:

    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) }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a program with C++/CLI that reads some data from
I am trying to create some charts of data (eg http://www.amibroker.com/ ). Is there
I am trying to create some dynamic html out of some data from db.
I'm trying to create some fixture data for my unit tests in CakePHP (via
I am currently trying to seed some test data using the following code: #
In the process of learning C... trying to create a function for some test
I am trying to mavenize my grails 2.1 app which uses build-test-data-2.0.3, for some
I am trying to create a function that will pass some parameters to a
I am trying to create unit tests for a REST client that does some
I'm trying to create a Matplotlib animation of my paw data, where you can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.