开发者

rails find - condition on array of db-fields

开发者 https://www.devze.com 2022-12-12 15:13 出处:网络
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.

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开发者_JAVA百科_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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号