访问连接模型值的正确方法是什么?

发布于 2024-12-10 09:46:58 字数 192 浏览 1 评论 0原文

我有用户和项目,它们通过称为所有权的 has_many :through 模型关联。在项目显示页面上,我想列出项目的名称、与项目关联的用户以及关联类型(在所有权加入模型中称为owner_type)。但我不太清楚该怎么做。

另外,如果我有一个项目和 current_user,我如何确定该项目的该用户的owner_type 是什么?

谢谢!

I have users and projects and they are associated through a has_many :through model called ownerships. On the project show page, I would like to list the name of the project, the users associated with the project, and the type of association (called owner_type in the ownership join model). I can't quite figure out how to do that though.

Also, if I have a project and the current_user, how do I figure out what the owner_type is for that user to that project?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-12-17 09:46:58

我认为可以安全地假设在项目显示中定义了@project。正确的?因此,可能的视图可能类似于...

app/views/projects/show.html.erb

<h1><%= @project.name %></h1>
<ul>
    <%= render :partial => 'ownership', :collection => @project.ownerships %>
</ul>

app/views/projects/_ownership.html.erb

<li><strong><%= ownership.user.name %></strong> (<%= ownership.ownership_type %>)</li>

但我不明白你的第二个问题。我建议将其作为一个完全独立的问题发布,也许需要更多说明。

I think it's safe to assume that in the project show, @project is defined. Correct? So, a possible view could look something like...

app/views/projects/show.html.erb

<h1><%= @project.name %></h1>
<ul>
    <%= render :partial => 'ownership', :collection => @project.ownerships %>
</ul>

app/views/projects/_ownership.html.erb

<li><strong><%= ownership.user.name %></strong> (<%= ownership.ownership_type %>)</li>

I don't understand your second question though. I suggest posting that as an entirely separate question, maybe with some more clarification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文