Label references for associations

两盒软妹~` 提交于 2021-02-07 18:11:11

问题


Rails provides label references for associations in fixtures like this:

### in pirates.yml

  reginald:
    name: Reginald the Pirate
    monkey: george

### in monkeys.yml

  george:
    name: George the Monkey
    pirate: reginald

This works great for not-namedspaced models, but I'm using namespaces, and so Rails gets confused, and want to insert the labels instead of the label references.

Any workaround or fix known?


回答1:


Fixtures.identify seems to be the only solution, not really beautiful but better than ids.

### in pirates.yml

  reginald:
    name: Reginald the Pirate
    monkey_id: <%= Fixtures.identify(:george) %>


### in monkeys.yml

  george:
    name: George the Monkey
    pirate_id: <%= Fixtures.identify(:reginald) %>


来源:https://stackoverflow.com/questions/6738737/label-references-for-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!