I have an orders object with a status field. I would like to retrieve all orders with status=1 (so I know what is being modified) and then update them to have a status=2. My code is as follows:
@new_orders=Order.where("status=1")
Order.where("status=1").update_all :status=>2
The problem is that @new_orders is not set until the view uses the variable. I am guessing this has to do with lazy load开发者_JAVA技巧ing. How do I get around this so I can display all records that have been modified?
Try adding .all
or .to_a
to the end of your relation:
@new_orders = Order.where(:status => 1).all
精彩评论