处理类别/子类别关系Ruby on Rails的最佳方法

时间:2022-03-03 07:37:29

I have a Category and Subcategory model. Category has many subcategories and Subcategory belongs to Category. Each subcategory currently has a position field to keep track of where it lines up under the Category it belongs to.

我有一个Category和Subcategory模型。类别有许多子类别,子类别属于类别。每个子类别当前都有一个位置字段,用于跟踪它所属的类别下的排队位置。

The problem with the way i'm doing it is that each I need the :position field to be unique to each category. So instead of using validates_uniqueness_of :position in my Subcategory model, I need a way to check that the position field is unique to its specific Category. Anyone have any ideas, or possibly dealt with this issue before?

我这样做的问题是每个我需要:position字段对每个类别都是唯一的。因此,我需要一种方法来检查位置字段对于其特定类别是唯一的,而不是在我的子类别模型中使用validates_uniqueness_of:position。任何人有任何想法,或可能以前处理过这个问题?

3 个解决方案

#1


1  

Expanding on lucapettes answer,

扩展lucapettes答案,

A nested set (https://github.com/skyeagle/nested_set) is something like this :

嵌套集(https://github.com/skyeagle/nested_set)是这样的:

- category
+- category 
+- category 
+--- category 
+- category 
- category
+- category 
+- category 
+--- category 
+- category

You can add node to the set anywhere and you would associate your product with any category.

您可以在任何位置向集合添加节点,并将产品与任何类别相关联。

Have a look in this file for the methods https://github.com/skyeagle/nested_set/blob/master/lib/nested_set/base.rb

请查看此文件中的方法https://github.com/skyeagle/nested_set/blob/master/lib/nested_set/base.rb

The wiki for awsome_nested_set might have some better docs, https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet

awsome_nested_set的wiki可能有更好的文档,https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet

For example you can move an item left, (e.g. up in a list like this ) category.move_left

例如,您可以向左移动一个项目(例如,在这样的列表中)category.move_left

To understand this model and why it works, read Trees in SQL by Joe Celko

要了解此模型及其工作原理,请阅读Joe Celko的SQL in Trees

#2


1  

Use NestedSet for Category/Subcategory handling and ActsAsList for position handling.

使用NestedSet进行类别/子类别处理,使用ActsAsList进行位置处理。

#3


1  

I believe this validates uniqueness of name based on user id (i.e. name is unique per-user):

我相信这会根据用户ID验证名称的唯一性(即名称是每用户唯一的):

class Report < ActiveRecord::Base
  validates_uniqueness_of :name, :scope => [:user_id]
end

A simple association as has_many and belongs_to may work for you. If position is an integer, you can do order_by :position in your controller with or without uniqueness of that position.

has_many和belongs_to之类的简单关联可能对您有用。如果position是一个整数,你可以在你的控制器中执行order_by:position,有或没有该位置的唯一性。

#1


1  

Expanding on lucapettes answer,

扩展lucapettes答案,

A nested set (https://github.com/skyeagle/nested_set) is something like this :

嵌套集(https://github.com/skyeagle/nested_set)是这样的:

- category
+- category 
+- category 
+--- category 
+- category 
- category
+- category 
+- category 
+--- category 
+- category

You can add node to the set anywhere and you would associate your product with any category.

您可以在任何位置向集合添加节点,并将产品与任何类别相关联。

Have a look in this file for the methods https://github.com/skyeagle/nested_set/blob/master/lib/nested_set/base.rb

请查看此文件中的方法https://github.com/skyeagle/nested_set/blob/master/lib/nested_set/base.rb

The wiki for awsome_nested_set might have some better docs, https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet

awsome_nested_set的wiki可能有更好的文档,https://github.com/collectiveidea/awesome_nested_set/wiki/Awesome-nested-set-cheat-sheet

For example you can move an item left, (e.g. up in a list like this ) category.move_left

例如,您可以向左移动一个项目(例如,在这样的列表中)category.move_left

To understand this model and why it works, read Trees in SQL by Joe Celko

要了解此模型及其工作原理,请阅读Joe Celko的SQL in Trees

#2


1  

Use NestedSet for Category/Subcategory handling and ActsAsList for position handling.

使用NestedSet进行类别/子类别处理,使用ActsAsList进行位置处理。

#3


1  

I believe this validates uniqueness of name based on user id (i.e. name is unique per-user):

我相信这会根据用户ID验证名称的唯一性(即名称是每用户唯一的):

class Report < ActiveRecord::Base
  validates_uniqueness_of :name, :scope => [:user_id]
end

A simple association as has_many and belongs_to may work for you. If position is an integer, you can do order_by :position in your controller with or without uniqueness of that position.

has_many和belongs_to之类的简单关联可能对您有用。如果position是一个整数,你可以在你的控制器中执行order_by:position,有或没有该位置的唯一性。