0

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])
3
  • What is size? an attribute on the model? Another model? If an attribute what you've put should work Commented Feb 26, 2015 at 17:07
  • An attribute on the model Commented Feb 26, 2015 at 17:07
  • 2
    This shows no research. If you read the ActiveRecord docs, or simply play around in the Rails console, you would easily find an answer. Commented Feb 26, 2015 at 17:08

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

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.