Take the 2-minute tour ×

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 %>

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


share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.


Then use this method in your grouped_collection_select.

<%= c.grouped_collection_select :product_id, @categories, :target_products, :name, :id, :name, :include_blank => true %>
share|improve this answer
Thanks A LOT, I was trying this since a long time. –  user2072365 May 20 '13 at 7:45
No prob. Sorry for the initial confusion on the initial answer. –  miked May 20 '13 at 14:34
add comment

To do this, you would want to build your collection based on that criteria. Meaning, @categories would only contain those categories with the products that meet your condition.

(I'm making some assumptions about your schema but something like this should work:)

@categories = Category.where("products.some_attribute = ?", true).joins(:products)
share|improve this answer
This was already what I made but the problem is that ALL the products are here for this category, even those which are "false"... –  user2072365 May 16 '13 at 12:36
Hm. Sounds like you're doing something wrong. The resulting query that's built from this code would only return those products with "some_attribute" as true (or 1). Can you post your code that sets @category? –  miked May 16 '13 at 23:48
The code you wrote is "give me only categories with some products with attribut equals to true. But grouped_collection_select will take all the products from those categories ! So yes I only have categories with at least one product 'true', but also with 'false' because it is never precised in grouped_collection_select. –  user2072365 May 17 '13 at 16:25
One of us is confused. The select can only build itself based on what it's provided with. So, if the query selects only those products (and associated categories) that have the flag set to 'true' it can not possibly contain those that are false. Can you please show me how @categories is being set?! –  miked May 17 '13 at 16:52
I don't understand what you want me to show you, @categories is set with your code "@categories = Category.where("products.some_attribute = ?", true).joins(:products)" and then I use the grouped_collection_select in my first post. –  user2072365 May 17 '13 at 17:07
show 2 more comments

Your Answer


