导轨装置上的红宝石中的自动关联 [英] Automatic associations in ruby on rails fixtures

查看:46
本文介绍了导轨装置上的红宝石中的自动关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文所述,我在固定装置.例如,如果区域对象具有国家/地区ID,而不是执行"country_id":1,则执行"country":"USA". "USA"是我的country.yml文件中的标签,因此固定装置知道如何处理.但是,仅当您未为国家对象指定ID值时,这才起作用.因此,我无法将USA的ID分配为1.但是,如果我不将其分配为1,则最终会得到一些较大的值8974343 ...这有点奇怪.有没有办法让灯具自动生成不是很高的ID? ....还是可以吗?

As described in this article, I am using automatic associations in fixtures. For example, if a region object has a country id, instead of doing "country_id": 1, I do "country": "USA". "USA" is a label in my countries.yml file, so fixtures knows how to take care of this. However, this only works when you do not specify an ID value for the countries object. So I cannot assign USA's ID to be 1. But if I do not assign it to be 1, it ends up being some large value 8974343...which is kinda strange. Is there a way to get fixtures to auto-generate id's that are not super high? ....or is this ok?

推荐答案

阅读API文档,这正是自动生成的灯具应该表现的方式-如果您想预先为灯具指定特定的ID值,则可以应该应该自己分配.

Reading the API documentation, this is exactly how autogenerated fixtures are supposed to behave -- if you want to have a specific ID value for a fixture in advance, you should probably just assign it yourself.

如果不是,请参阅API文档:

If not, well, from the API docs:

The generated ID for a given label is constant, so we can discover any fixture‘s ID without loading anything, as long as we know the label.

这篇关于导轨装置上的红宝石中的自动关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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