与不一致的固定装置混淆
Confusion with inconsistent fixtures
我的固定装置出了点问题导致它们表现异常。 "Score" 模型将 "Users" 与 "Assignments" 连接在一起,并添加了一些其他信息的列。夹具如下所示:
Score.yml
one:
assignment: pythag
position: 2
student: natalie
points: 75
这会产生错误:ActiveRecord::Fixture::FixtureError:table "scores" 没有名为 "assignment".
的列
这个错误是有道理的,因为我的分数模式包括一个名为“:student_id”的列,而不是一个名为“:student”的列。
但我很困惑,因为我所有的其他灯具都使用与我在这里尝试使用的相同类型的相对键。例如,我的 "assignments" fixtures 指的是 "seminar," 而不是 "seminar_id."
assignment.yml
one:
position: 1
name: Pythag
possible: 100
seminar: one
这使我可以轻松地从我的 seminars.yml 文件中引用特定的固定装置,这样我就不需要找到 rails 产生的奇怪 ID 号。我想对我的比分装置做同样的事情。
关于什么可能导致一个装置需要绝对列名,而其他装置很容易接受相对列名的任何想法?
提前感谢您的任何见解。
-杰夫
您应该在 Score
模型中定义关联。看看documentation,尤其是这部分:
Active Record reflects on the fixture's model class, finds all the belongs_to associations, and allows you to specify a target label for the association (monkey: george) rather than a target id for the FK (monkey_id: 1)
我的固定装置出了点问题导致它们表现异常。 "Score" 模型将 "Users" 与 "Assignments" 连接在一起,并添加了一些其他信息的列。夹具如下所示:
Score.yml
one:
assignment: pythag
position: 2
student: natalie
points: 75
这会产生错误:ActiveRecord::Fixture::FixtureError:table "scores" 没有名为 "assignment".
的列这个错误是有道理的,因为我的分数模式包括一个名为“:student_id”的列,而不是一个名为“:student”的列。
但我很困惑,因为我所有的其他灯具都使用与我在这里尝试使用的相同类型的相对键。例如,我的 "assignments" fixtures 指的是 "seminar," 而不是 "seminar_id."
assignment.yml
one:
position: 1
name: Pythag
possible: 100
seminar: one
这使我可以轻松地从我的 seminars.yml 文件中引用特定的固定装置,这样我就不需要找到 rails 产生的奇怪 ID 号。我想对我的比分装置做同样的事情。
关于什么可能导致一个装置需要绝对列名,而其他装置很容易接受相对列名的任何想法?
提前感谢您的任何见解。
-杰夫
您应该在 Score
模型中定义关联。看看documentation,尤其是这部分:
Active Record reflects on the fixture's model class, finds all the belongs_to associations, and allows you to specify a target label for the association (monkey: george) rather than a target id for the FK (monkey_id: 1)