propster
propster

Reputation: 577

Find multiple objects by attribute values

I have Products.

Products have unique sizes. Sizes are attributes.

I want to change the status of Products with sizes 10, 20 and 30 exclusively.

How do I query them in one go?

I looking for something as simple as:

Products.find_by(size: [10, 20, 30])

Upvotes: 0

Views: 785

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Well you can use #update_all and #update

# if you don't want to confirm validation
Products.where(size: [10, 20, 30]).update_all(status : 'x')
# if you do want to confirm validation
Products.where(size: [10, 20, 30]).find_each do |product|
 product.update(status : 'x')
end

Upvotes: 3

Related Questions