0

I have an ItemsDesign model with a HABTM with ItemsCategory and Color, belongs to ItemsStyle and Store

The Page is set up where you would select a category or subcategory. then you would have the ability to filter by color, store, style and price (price is a column in the items_design table)

I'm a little stuck on how to write this query. this is what I have so far. So when I introduced color into the mix it returns nothing.

Right now I'm just hard coding values in to get it work. Of course I need this to work with params.

@products = Kaminari.paginate_array(
              ItemsDesign.joins(:items_categories, :colors)
                .where('items_categories.id' => @category, 'colors.name' => :white))
              .page(params[:page]).per(48)

Here's the sql

SELECT "items_designs".* 
FROM "items_designs" 
INNER JOIN "items_categories_items_designs" 
  ON "items_categories_items_designs"."items_design_id" = "items_designs"."id" 
INNER JOIN "items_categories" 
  ON "items_categories"."id" = "items_categories_items_designs"."items_category_id" 
INNER JOIN "colors_items_designs" 
  ON "colors_items_designs"."items_design_id" = "items_designs"."id" 
INNER JOIN "colors" ON "colors"."id" = "colors_items_designs"."color_id" 
WHERE "items_categories"."id" IN (1, 5, 10, 11, 12, 6, 13, 14, 15, 7, 16, 17, 18, 19, 8, 20, 21, 22, 9, 23, 24, 25, 104) 
  AND "items_designs"."colors" = 'white'):
Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62
bonum_cete
  • 4,730
  • 6
  • 32
  • 56
  • This post answers your question: http://stackoverflow.com/questions/4480088/rails-search-with-optional-parameters – Sixty4Bit Jun 11 '14 at 23:17
  • This definitely looks useful from the different filter params standpoint. I'm not sure how to make it work with my relations though. – bonum_cete Jun 11 '14 at 23:47

0 Answers0