Rails fixtures — how do you set foreign keys?

前端 未结 2 1988
傲寒
傲寒 2020-12-16 10:37

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

2条回答
  •  眼角桃花
    2020-12-16 11:06

    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
    

提交回复
热议问题