帮助自引用模型和在 Rails 3 中查看

发布于 2024-10-06 15:12:36 字数 370 浏览 3 评论 0原文

我有一个简单的客户端表(或模型),其中客户端可以有一个父客户端(只有一个或没有)。我这样建模:

class Client < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Client', :foreign_key => 'parent_id’
end

看起来效果很好。我有两个问题:

  1. 这是在 R3 模型中表示该关系的正确原因吗?
  2. _form.html.erb 应该是什么样子?例如,我想要一个下拉框列出所有可能的客户端(和无)作为父字段。

I have a simple Client table (or model) where a Client can have a Parent Client (only one, or none). I modeled it this way:

class Client < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Client', :foreign_key => 'parent_id’
end

That seems to work fine. I have two questions:

  1. Is that the correct why to represent that relationship in an R3 model.
  2. What should the _form.html.erb look like? For example, I want a drop-down box listing all of the possible Clients (and None) as the Parent field.

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

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

发布评论

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

评论(2

陈独秀 2024-10-13 15:12:36

我找到了#2 的解决方案:

<div class="field">
   <%=f.label :parent %><br/>
   <%= collection_select(:client, :parent_id, Client.all, :id, :name, {:include_blank => true} ) %>
</div>

I found the solution to #2:

<div class="field">
   <%=f.label :parent %><br/>
   <%= collection_select(:client, :parent_id, Client.all, :id, :name, {:include_blank => true} ) %>
</div>
∞觅青森が 2024-10-13 15:12:36

是的,#1 是正确的。抱歉,刚刚意识到这个问题有多老了!

Yes it is, number #1 is correct. Sorry, just realised how old this question is!

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