grouped_collection_select与特定对象 [英] grouped_collection_select with particular objects

查看:55
本文介绍了grouped_collection_select与特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些产品(链接到类别)的grouped_collection_select:

I have a grouped_collection_select with some Products (linked to a category):

<%= c.grouped_collection_select :product_id, @categories, :products, :name, :id, :name, :include_blank => true %>

但是我只想要一些布尔值为"true"的产品,是否可以指定呢?

But I just want some products which have a boolean to "true", is it possible to specify this ?

谢谢

推荐答案

您基本上是在为分组的select中的每个父对象的实例调用一个方法.因此,如果您具有与所需产品相对应的父对象(类别)的实例方法,则可以在帮助器中调用它.像这样:

You're basically calling a method for an instance of each parent object in the grouped select. So if you have an instance method for the parent object (a Category) that corresponds to the products you need, you can call it in the helper. Something like this:

Class Category
  has_and_belongs_to_many :products

  def target_products
    #return a collection here that corresponds to what you're looking for based on
    #your criteria in the associated product(s) object. For example:
    self.products.active  #where active is a named scope in Product for what you're looking for.
    #You could also add an AR macro method with a condition etc. Basically any thing that 
    #will respond to category.target_products in the end.
  end

end 

然后在您的grouped_collection_select中使用此方法.

Then use this method in your grouped_collection_select.

<%= c.grouped_collection_select :product_id, @categories, :target_products, :name, :id, :name, :include_blank => true %>

这篇关于grouped_collection_select与特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆