Hel*_*iro 5 unit-testing ruby-on-rails fixtures
在不同模型之间的关联中,可以通过使用夹具名称来避免直接设置外键 ID,如本答案中所示。自引用关联怎么样,例如使用acts_as_tree时?尝试这个:
# categories.yml
forsale:
name: For Sale
parent_id: nil
books:
name: Books
parent: forsale
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
SQLite3::SQLException: table categories has no column named parent: INSERT INTO "categories" ("name", "parent") VALUES ('Books', 'forsale')
Run Code Online (Sandbox Code Playgroud)
有没有办法让一个灯具在同一类中引用另一个灯具而不使用显式 ID?
更新:
在括号之间附加类名(如多态的 own_to固定装置)也不起作用。这样做:
books:
name: Books
parent: forsale (Category)
Run Code Online (Sandbox Code Playgroud)
生成一个随机的parent_id代替books的forsaleID。
我已经这样做了好几次了,但不知道为什么它对你不起作用。当你拥有它们时,这些装置看起来是正确的(没有多态(Category))。该关联在您的应用程序的其余部分中起作用吗?您使用什么版本的 Rails?你的模型中应该有这样的东西Category:
belongs_to :parent, :class_name => "Category"
Run Code Online (Sandbox Code Playgroud)
如果你想强制它工作,你可以parent_id像这样设置一个显式:
books:
name: Books
parent_id: <%= Fixtures.identify :forsale %>
Run Code Online (Sandbox Code Playgroud)
但这显然不太理想......
| 归档时间: |
|
| 查看次数: |
1437 次 |
| 最近记录: |