Rails固定装置-如何设置外键? [英] Rails fixtures -- how do you set foreign keys?

查看:79
本文介绍了Rails固定装置-如何设置外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此指南中,我正在阅读有关Rails灯具的信息(谢谢,trevorturk).似乎您在Yaml文件中定义了类,然后将它们自动加载到test DB中-很酷.

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.

但是,如果您想指定此食谱属于该食谱(或其他内容),您该怎么做?

But if you want to specify that this recipe belongs to that cookbook (or whatever) how do you do that?

您是否应该在Yaml代码中手动指定cookbook.idrecipe.cookbook_id的值? (只是一个猜测-该指南没有显示类似的内容.)还是有一种更合适的方法?

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?

推荐答案

您应使用命名的灯具,该灯具会自动为您生成一个ID号(如果您没有提供ID号).这些ID号本质上是您使用的任何字符串的整数散列.如果您引用的是命名版本,请不要添加"_id":

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:

# recipes.yml
chicken_soup:
  cookbook: my_recipes

# cookbooks.yml
my_recipes:
  title: My Test Cookbook

这篇关于Rails固定装置-如何设置外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆