在 Rails 应用程序中处理类别的好方法是什么?

发布于 2024-11-17 09:40:38 字数 1302 浏览 1 评论 0原文

我正在尝试了解如何处理我的产品<->类别关系。 我正在尝试在 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 技术交流群。

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

发布评论

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

评论(2

酒中人 2024-11-24 09:40:38

您可以通过在模型中使用 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.

叹沉浮 2024-11-24 09:40:38

所以我最终通过分类创建了多对多关系。这个 railscast 完美地解释了如何执行此操作并创建新类别(或标签) )即时。

在我循环遍历类别以使它们链接到我的产品概述中之后:

# app/views/products/index.html.erb 
<ul class="categories">
    <% for category in @categories %>
       <li><%= link_to category.name, :action => "index" , :category => category.id %></li>
    <% end %>
    </ul>

然后在控制器中,我从该类别构建产品(如果有的话):

# products_controller.rb
  def index
    if params[:category]
      @products = Category.find(params[:category]).products
    else
      @products = Product.scoped
    end
    @products = @products.where("title like ?", "%" + params[:title] + "%") if params[:title]
    @products = @products.order('title').page(params[:page]).per( params[:per_page] ? params[:per_page] : 25)
    @categories = Category.all
  end

当然有一种更优雅的方法来做到这一点,但现在这很糟糕。任何改进表示赞赏。

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:

# app/views/products/index.html.erb 
<ul class="categories">
    <% for category in @categories %>
       <li><%= link_to category.name, :action => "index" , :category => category.id %></li>
    <% end %>
    </ul>

and then in the controller I build the products from the category if there is any:

# products_controller.rb
  def index
    if params[:category]
      @products = Category.find(params[:category]).products
    else
      @products = Product.scoped
    end
    @products = @products.where("title like ?", "%" + params[:title] + "%") if params[:title]
    @products = @products.order('title').page(params[:page]).per( params[:per_page] ? params[:per_page] : 25)
    @categories = Category.all
  end

for sure there is a more elegant way to do it but this wors for now.. any improvement appreciated.

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