views:

320

answers:

1

I am stuck and I guess it is a syntax thing I just don't get:

It's a one(category)_to_many(product) relationship.

In my category model I have the columns pick1, pick2, pick3, pick4, pick5. Each of those holds the id of a product.

In my category_controller I want to retrieve those in a find:

@productpicks = Product.find(:all, :conditions => 
  ['online = ? and category_id IN (?)', true,
  [@category.pick1, @category.pick2, @category.pick3, @category.pick4, @category.pick5]])

... and iterate over them in the view like this:

<% for product in @productpicks %>do something<% end %>

But there is nothing to be found in that array ...

Does anyone have an idea what I am doing wrong?

Thanks for helping!

Val

+1  A: 

Shouldn't it be:

@productpicks = Product.find(
  :all,
  :conditions => [
    'online = ? and id IN (?)',
    true, [
      @category.pick1,
      @category.pick2,
      @category.pick3,
      @category.pick4,
      @category.pick5
    ]
  ]
)

Replacing category_id with id in the where clause?

As pick1-5 hold product ids, and you are trying to find those specific products.

Tony Fontenot
Hey Tony! Thank you so much! You can't imagine for how long I have been thinking about this. Stupid me ;-)
val_to_many
No worries. Glad I can help.
Tony Fontenot