Ruby on rails Rails 5:如何将设备指向其他设备?
I三种模型Ruby on rails Rails 5:如何将设备指向其他设备?,ruby-on-rails,ruby-on-rails-5.2,Ruby On Rails,Ruby On Rails 5.2,I三种模型注释,用户和项目项目和注释需要指向其他对象才能有效。例如,注释需要指向作者(用户)和项目 关联的装置文件如下所示: # comments.yml test_comment: author: users(:test_user) project: projects(:test_project) # users.yml test_user: name: 'test user' # projects.yml test_project: name: 'Test' desc
注释
,用户
和项目
<代码>项目和注释需要指向其他对象才能有效。例如,注释需要指向作者(用户)和项目
关联的装置文件如下所示:
# comments.yml
test_comment:
author: users(:test_user)
project: projects(:test_project)
# users.yml
test_user:
name: 'test user'
# projects.yml
test_project:
name: 'Test'
description: 'This is a test'
owner: users(:test_user)
然而,我发现我的设备可能设置不正确。如果我尝试保存注释,Rails将返回false
:
assert_equal true, comments(:test_comment)
#=> false
我可以看到项目和作者有外键:
=> #<Comment:0x00007f9b1661f3d8
id: 137725605,
body: "",
project_id: 745075726,
author_id: "31ceee04-5307-5059-91db-0dc2068a780c",
created_at: Fri, 22 Feb 2019 13:17:58 UTC +00:00,
updated_at: Fri, 22 Feb 2019 13:17:58 UTC +00:00>
我希望其中一个将返回用户(:test\u user)
,另一个将返回项目(:test\u project)
。我想也许我需要在yaml中使用ERB:
test_comment:
author: <%= users(:test_user) %>
project: <%= projects(:test_project) %>
test\u注释:
作者:
项目:
但当我运行测试时,结果是一连串的错误:
NoMethodError: undefined method `users' for #<#<Class:0x00007f9b17692ff8>:0x00007f9b17692dc8>
NoMethodError:#
我需要做什么才能将装置指向其他装置?能做到吗?我做错了什么?在中,您可以看到不需要用户(:test\u user)
来引用其他对象。相反,您只需编写test\u user
:
# comments.yml
test_comment:
author: test_user
project: test_project
# users.yml
test_user:
name: 'test user'
# projects.yml
test_project:
name: 'Test'
description: 'This is a test'
owner: test_user
希望这有帮助
# comments.yml
test_comment:
author: test_user
project: test_project
# users.yml
test_user:
name: 'test user'
# projects.yml
test_project:
name: 'Test'
description: 'This is a test'
owner: test_user