简体   繁体   English

Rails 2.0装置中的外键

[英]Foreign keys in fixtures in rails 2.0

I'm having problems with my fixtures in Rails. 我在Rails中的固定装置遇到问题。

I have two models, a message and a user. 我有两个模型,一个消息和一个用户。 The user model is generated from restful authentication. 用户模型是通过静态身份验证生成的。 A message has a sender and a recipient, which are both users. 邮件有一个发送者和一个接收者,它们都是用户。

When I don't use the fixtures everything works fine. 当我不使用灯具时,一切正常。 But when I try to use them in fixtures both are nil. 但是,当我尝试在固定装置中使用它们时,两者都是零。

This is the messages fixtures file: 这是消息固定文件:

first_message_for_quentin:
    title: First message
    body: This is your first message.
    recipient: quentin
    sender: aaron

quentin and aaron are the user fixtures which were generated by restful authentication. quentin和aaron是通过静态身份验证生成的用户设备。

Is there a way I can use the generated ids there or do I have to do it another way? 有什么方法可以在其中使用生成的ID,还是必须以其他方式使用?

The ActiveRecord Fixtures creates the ID column for each row by calling Fixtures.identify on the label you gave the row in the yaml. ActiveRecord夹具通过在yaml中为行指定的标签上调用Fixtures.identify来为每一行创建ID列。

So you should be able to do something like the following: 因此,您应该能够执行以下操作:

first_message_for_quentin:
    title: First message
    body: This is your first message.
    recipient_id: <%= Fixtures.identify(:quentin) %>
    sender_id: <%= Fixtures.identify(:aaron) %>

You might want to look at some of the docs for fixture label interpolation. 您可能需要查看一些有关灯具标签插值的文档

The problem was in the fixture file which was created by restful authentication. 问题出在通过静态认证创建的装置文件中。

quentin:
    id: 1
    ...

aaron:
    id: 2
    ...

I removed these ids and everything is working fine. 我删除了这些ID,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM