I'm reading about Rails fixtures in this guide (thanks, trevorturk). It appears you define classes in a Yaml file and they're automatically loaded into the test
DB -- cool.
我正在阅读本指南中关于Rails装置的内容(谢谢,trevorturk)。看来你在Yaml文件中定义了类,它们会自动加载到测试数据库中 - 很酷。
But if you want to specify that this recipe belongs to that cookbook (or whatever) how do you do that?
但是如果你想指定这个食谱属于那个食谱(或其他什么),你怎么做?
Are you supposed to specify the values for cookbook.id
and recipe.cookbook_id
by hand in the Yaml code? (Just a guess -- the guide doesn't show anything like that.) Or is there a more suitable way?
您是否应该在Yaml代码中手动指定cookbook.id和recipe.cookbook_id的值? (只是一个猜测 - 指南没有显示那样的东西。)或者是否有更合适的方式?
2 个解决方案
#1
55
You should use named fixtures, which automatically generate an id number for you where you don't provide one. These id numbers are essentially integer hashes of whatever string you use. Don't add the "_id" if you're referencing the named version:
您应该使用命名的灯具,它会为您自动生成您不提供的ID号码。这些id号基本上是你使用的任何字符串的整数哈希值。如果您引用了指定版本,请不要添加“_id”:
# recipes.yml
chicken_soup:
cookbook: my_recipes
# cookbooks.yml
my_recipes:
title: My Test Cookbook
#2
3
Additionally, if you wish to have a many to many association ( HABTM ) you just give an array for the association in the fixture:
此外,如果您希望有多对多关联(HABTM),您只需为夹具中的关联提供一个数组:
# recipes.yml
chicken_soup:
cookbooks: [my_recipes, another_recipe]
#1
55
You should use named fixtures, which automatically generate an id number for you where you don't provide one. These id numbers are essentially integer hashes of whatever string you use. Don't add the "_id" if you're referencing the named version:
您应该使用命名的灯具,它会为您自动生成您不提供的ID号码。这些id号基本上是你使用的任何字符串的整数哈希值。如果您引用了指定版本,请不要添加“_id”:
# recipes.yml
chicken_soup:
cookbook: my_recipes
# cookbooks.yml
my_recipes:
title: My Test Cookbook
#2
3
Additionally, if you wish to have a many to many association ( HABTM ) you just give an array for the association in the fixture:
此外,如果您希望有多对多关联(HABTM),您只需为夹具中的关联提供一个数组:
# recipes.yml
chicken_soup:
cookbooks: [my_recipes, another_recipe]