嵌套设置宝石 - 我是否需要它以及如何限制深度(如果需要)

发布于 2025-01-08 14:28:07 字数 2377 浏览 1 评论 0原文

我正在尝试使用 Nested Set Gem 此处 创建我的应用程序 我正在尝试为我的应用程序创建一个简单的产品菜单,所以我会有这样的内容

+Category
--类别
----产品
----产品
--类别
----产品
----Product

我对嵌套 Set gem 的问题是,我不希望深度 2 超过两个类别的深度。默认情况下,嵌套 set gem 允许类似

+Category
--类别
---类别
----类别
-----产品

但是对于我们的 CSS 样式目的来说,我们不想处理那么深的内容,而且客户也不需要那么深的内容。

所以我想知道我是否首先需要嵌套集,或者标准 has_many own_to 在这种情况下工作吗?我正在使用此问题的答复中的嵌套集 此处

我个人认为我可能只使用标准的 has_many 并属于,除非有人可以告诉我在我的情况下使用嵌套集的优势。如果我确实需要它,如何限制客户只选择正确的深度来添加节点?

编辑:已解决,谢谢。 我的新问题是我收到错误

undefined method `self_and_descendants' for []:ActiveRecord::Relation 提取的源代码(第9行附近):

6: </p>
7: <p>
8:   <%= f.label(:parent_id) %>
9:   <%= f.select :parent_id, nested_set_options(Category.categories, @category) {|i,     level| "#{'-' * level} #{i.name}" } %>
10: </p>
11: <p>
12:   <% # f.label(:position) %>

views/categories/_form.html.erb

<%= form_for(@category) do |f| %>
<p>
  <%= f.label(:name) %>
  <%= f.text_field :name %>
</p>
<p>
  <%= f.label(:parent_id) %>
    <%= f.select :parent_id, nested_set_options(Category.categories, @category) {|i, level| "#{'-' * level} #{i.name}" } %>
</p>
<p>
  <% # f.label(:position) %>
  <% # f.select :position, 1..category_count %>
</p>
<p>
  <%= f.submit("Submit") %>
</p>
<% end %>

models/category.rb

class Category < ActiveRecord::Base
  acts_as_nested_set
  acts_as_list :scope => :parent_id
  has_many :products
  scope :position, order("position asc")
  scope :categories, where("parent_id IS NULL")
  scope :subcategories, where("parent_id IS NOT NULL")
end

routes.rb(因空间原因而缩短)

Locksmithing::Application.routes.draw do
resources :categories do
  collection { post :sort }
  resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory]
  end
end

I am trying to create my app using the Nested Set Gem here
I am trying to create a simple product menu for my app, so I would have something like this

+Category
--Category
----Product
----Product
--Category
----Product
----Product

My problem with the nested Set gem is that I don't want the depth two be more then two category's deep. The nested set gem by default allows something like this

+Category
--Category
---Category
----Category
-----Product

But for our CSS Styling purposes, we don't want to deal with that much depth, and the customer doesn't need that much depth to it.

So i'm wondering if I even need the nested set in the first place, or will a standard has_many belongs_to work in this scenario? I am using Nested Set from the responses to this question here.

I am personally under the opinion that I may just use a standard has_many and belongs to, unless someone can inform me an advantage to using Nested Set in my case. If I do need it, how do I limit the customer from only selecting the correct depth to add a node to?

EDIT: Solved, Thanks.
My new problem is I am getting the error

undefined method `self_and_descendants' for []:ActiveRecord::Relation
Extracted source (around line #9):

6: </p>
7: <p>
8:   <%= f.label(:parent_id) %>
9:   <%= f.select :parent_id, nested_set_options(Category.categories, @category) {|i,     level| "#{'-' * level} #{i.name}" } %>
10: </p>
11: <p>
12:   <% # f.label(:position) %>

views/categories/_form.html.erb

<%= form_for(@category) do |f| %>
<p>
  <%= f.label(:name) %>
  <%= f.text_field :name %>
</p>
<p>
  <%= f.label(:parent_id) %>
    <%= f.select :parent_id, nested_set_options(Category.categories, @category) {|i, level| "#{'-' * level} #{i.name}" } %>
</p>
<p>
  <% # f.label(:position) %>
  <% # f.select :position, 1..category_count %>
</p>
<p>
  <%= f.submit("Submit") %>
</p>
<% end %>

models/category.rb

class Category < ActiveRecord::Base
  acts_as_nested_set
  acts_as_list :scope => :parent_id
  has_many :products
  scope :position, order("position asc")
  scope :categories, where("parent_id IS NULL")
  scope :subcategories, where("parent_id IS NOT NULL")
end

routes.rb (Shortened for space)

Locksmithing::Application.routes.draw do
resources :categories do
  collection { post :sort }
  resources :children, :controller => :categories, :only => [:index, :new, :create, :new_subcategory]
  end
end

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

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

发布评论

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

评论(1

枯寂 2025-01-15 14:28:07

在这种情况下你绝对可以继续使用nested_set。

我当前正在开发的应用程序中有一个分层设置,虽然我允许无限嵌套,但我只希望导航显示前 2 层。

我有一个命名范围,让我限制我正在查看的内容:

scope :with_depth_below, lambda { |level|
  where(self.arel_table[:depth].lt(level))
}

这样(使用示例中的类别)您可以使用

Category.with_depth_below(4).arrange

您可以将该范围传递到任何nested_set的范围中 - 或者实际上是您可能想要的View Helper用于限制它可以达到的级别。

为了真正限制它,我还只是放置一个验证约束... before_save,检查深度是否小于最大深度。有道理吗?

You could definitely continue using nested_set in this kind of context.

I have a hierarchical setup in the app I'm currently working on and while I allow infinite nesting, I only want the nav to show top 2 levels.

I have a named scope to let me limit what I'm looking at:

scope :with_depth_below, lambda { |level|
  where(self.arel_table[:depth].lt(level))
}

Such that (using Category from your example) you could use

Category.with_depth_below(4).arrange

You can pass that scope into any of nested_set's scopes - or indeed the View Helper you're probably aiming to use for limiting how many levels it can go.

To genuinely limit it, I'd also just place a validation constraint... before_save, checking that the depth is less than your max depth. Make sense?

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