Ruby on rails 3 rails 3-FactoryGirl创建关联记录
我正在尝试创建一些测试数据来填充我的表,以便在我的站点上测试功能 有问题的表格是:Ruby on rails 3 rails 3-FactoryGirl创建关联记录,ruby-on-rails-3,associations,factory-bot,Ruby On Rails 3,Associations,Factory Bot,我正在尝试创建一些测试数据来填充我的表,以便在我的站点上测试功能 有问题的表格是:歌曲,歌曲安排,以及歌曲安排文件。这些数据以这种方式关联 Songs: has_many :song_arrangements, :dependent => :destroy has_many :song_arrangement_files, :through => :song_arrangements Song Arrangements: belongs_to :song
歌曲
,歌曲安排
,以及歌曲安排文件
。这些数据以这种方式关联
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
关于如何创建与各自的歌曲安排
或歌曲安排
记录正确关联的歌曲安排
和歌曲安排
文件有何想法
我猜我可以在工厂中嵌套(:create)之后使用
。我对FactoryGirl非常陌生,对Rails总体来说还是相当陌生的。非常感谢您的帮助 对于其他遇到这个问题的人,我提出了一个解决方案。正如我所说,我是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
FactoryGirl.define do
工厂:song do|s|
s、 顺序(:title){n}“歌曲标题{n}”
s、 序列(:艺术家){n}“歌曲艺术家{n}”
在(:create)do| song之前|
所以我的另一个答案帮助我做了我需要做的事情;然而,我基本上需要更深入地进行一个层次的关联,我正在用这个解决方案碰壁。最后,我重新阅读了FactoryGirl的关联文档,并提出了适用于我所有案例的解决方案。它创建歌曲、歌曲排列和歌曲排列文件。我肯定代码不漂亮,但它可以工作,以后可以改进。希望这能帮助任何遇到同样路障的人
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) }
不确定这是否重要,但我正在使用rspec进行测试。