0

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:

do something

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.