在rails 4应用程序中对产品进行分类的最佳方法 [英] Best way to categorize products in rails 4 app
问题描述
这是我到目前为止:
class Category< ActiveRecord :: Base
has_many:products,through::categorizations
has_many:categorizations
end
class Product< ActiveRecord :: Base
包括ActionView :: Helpers
$ b $ has_many:类别,通过::分类
has_many:分类
结束
类分类< ActiveRecord :: Base
belongs_to:category
belongs_to:product
end
<另外,我应该使用什么宝石? (awesome_nested_set,has_ancestry)
谢谢!
我在我的一个项目中做了这样的工作,它现在运行得非常好。
首先是类别模型,它有一个名称属性,我使用的是gem acts_as_tree
,以便类别可以有子类别。
class Category< ActiveRecord :: Base
acts_as_tree命令::name
has_many:categoricals
validates:name,uniqueness:{case_sensitive:false},presence:true
end
然后我们将添加一个名为 categorical
的模型,它是任何可分类
的实体(产品)和类别
。请注意, categorizable
是多态的。
类Categorical< ActiveRecord :: Base
belongs_to:category
belongs_to:categorizable,polymorphic:true
validates_presence_of:category,:categorizable
end
$ c $现在一旦我们建立了这两个模型,我们将添加一个关注点,它可以使任何实体 categorizable $ c> c $ c>在本质上,无论是产品,用户等。
模块可分类
扩展ActiveSupport :: Concern
包含做
has_many:categoricals,如::categorizable
has_many:categories,through::categoricals
end
def add_to_category(类别)
self.categoricals.create(category:category)
end
$ b $ def def_from_category(category)
self.categoricals.find_by(category:category).maybe .destroy
end
Module ClassMethods
end
end
现在我们只是将它包含在一个模型中,使它可以分类。
c产品< ActiveRecord :: Base
包括Categorizable
end
这个
p = Product.find(1000)#返回一个产品,Ferrari
c = Category.find_by(name: 'car')#返回类别车
p.add_to_category(c)#相互关联
p.categories#将返回产品所属的所有类别
So, I'm trying to create a product categorization 'system' in my rails 4 app.
Here's what I have so far:
class Category < ActiveRecord::Base
has_many :products, through: :categorizations
has_many :categorizations
end
class Product < ActiveRecord::Base
include ActionView::Helpers
has_many :categories, through: :categorizations
has_many :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
Also, what gem should I use? (awesome_nested_set, has_ancestry)
Thanks!
解决方案 This is what I did in one of my projects which is live right now and works very well.
First the category model, it has a name attribute and I am using a gem acts_as_tree
so that categories can have sub categories.
class Category < ActiveRecord::Base
acts_as_tree order: :name
has_many :categoricals
validates :name, uniqueness: { case_sensitive: false }, presence: true
end
Then we will add something called a categorical
model which is a link between any entity(products) that is categorizable
and the category
. Note here, that the categorizable
is polymorphic.
class Categorical < ActiveRecord::Base
belongs_to :category
belongs_to :categorizable, polymorphic: true
validates_presence_of :category, :categorizable
end
Now once we have both of these models set up we will add a concern that can make any entity categorizable
in nature, be it products, users, etc.
module Categorizable
extend ActiveSupport::Concern
included do
has_many :categoricals, as: :categorizable
has_many :categories, through: :categoricals
end
def add_to_category(category)
self.categoricals.create(category: category)
end
def remove_from_category(category)
self.categoricals.find_by(category: category).maybe.destroy
end
module ClassMethods
end
end
Now we just include it in a model to make it categorizable.
class Product < ActiveRecord::Base
include Categorizable
end
The usage would be something like this
p = Product.find(1000) # returns a product, Ferrari
c = Category.find_by(name: 'car') # returns the category car
p.add_to_category(c) # associate each other
p.categories # will return all the categories the product belongs to
这篇关于在rails 4应用程序中对产品进行分类的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!