在 Rails 应用程序中处理类别的好方法是什么?
我正在尝试了解如何处理我的产品<->类别关系。 我正在尝试在 Rails 中建立一个小商店,我想从类别树中进行导航。
导航将如下所示:
- Men
|--Shirts
|--Pants
- Woman
|--Shirts
|--Dresses
-Accessoires
您明白了...
现在的问题是,这些似乎都是同一模型“产品”上的不同范围,并且在关联的类别上具有不同的查找条件。
到目前为止我的模型:
class Product < ActiveRecord::Base
# validations...
has_many :categorizations
has_many :categories, :through => :categorizations
# more stuff ...
end
class Category < ActiveRecord::Base
acts_as_nested_set
has_many :categorizations
has_many :products, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
此外,我希望我的产品有多个类别,并且也许可以在添加产品时“即时”创建新类别。所以整个品类的管理应该尽可能的简单。如果有人能给我指出正确的方向或将我链接到教程、最佳实践或其他任何东西,那就太棒了!
更新
好的,现在我可以使用 虚拟属性,问题是如何搜索特定类别的文章?
我尝试过:
@products = Product.scoped(:include => :categorizations, :conditions => {:category_names => params[:category]})
或者
@products = Product.where("categorization = ?", params[:category])
两者都不起作用。基本上我想要一个类别的所有产品......
I am trying to get my head around how to deal with my Products <-> Categories relation.
I am trying to build a small shop in rails and I want to make a navigation out of the category tree.
The navigation will look something like this:
- Men
|--Shirts
|--Pants
- Woman
|--Shirts
|--Dresses
-Accessoires
You get the idea...
Now, the problem is that these appear to be all different scopes on the same model, Product, with different find conditions on the associated Category.
My models so far:
class Product < ActiveRecord::Base
# validations...
has_many :categorizations
has_many :categories, :through => :categorizations
# more stuff ...
end
class Category < ActiveRecord::Base
acts_as_nested_set
has_many :categorizations
has_many :products, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
Also, I want to have multiple categories on my products and maybe make it possible to create new categories "on-the-fly" when adding a product. So the whole category management should be as easy as possible. If someone can point me in the right direction or link me to a tutorial, best practice or anything would be really awesome!
UPDATE
Ok, so now I can creating categories on the fly using virtual attributes, the question is how do I search for articles of a specific category?
What I tried:
@products = Product.scoped(:include => :categorizations, :conditions => {:category_names => params[:category]})
or
@products = Product.where("categorization = ?", params[:category])
but both didnt work. basically i want all products of one category...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在模型中使用
accepts_nested_attributes_for
允许用户在创建新产品的同时创建新类别。查看文档以帮助您入门。You can allow users to create new categories at the same time as creating new products by using
accepts_nested_attributes_for
in your model. Have a look through the documentation for that to get you started.所以我最终通过分类创建了多对多关系。这个 railscast 完美地解释了如何执行此操作并创建新类别(或标签) )即时。
在我循环遍历类别以使它们链接到我的产品概述中之后:
然后在控制器中,我从该类别构建产品(如果有的话):
当然有一种更优雅的方法来做到这一点,但现在这很糟糕。任何改进表示赞赏。
So I ended up creating a many-to-many relation through categorizations. This railscast explains perfectly how to do this and create new categories (or tags) on-the-fly.
After I loop through the categories to make them links in my product overview:
and then in the controller I build the products from the category if there is any:
for sure there is a more elegant way to do it but this wors for now.. any improvement appreciated.