Dan
Dan

Reputation: 1

rails active record persistance problem

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 loading. How do I get around this so I can display all records that have been modified?

Upvotes: 0

Views: 47

Answers (1)

Peter Brown
Peter Brown

Reputation: 51697

Try adding .all or .to_a to the end of your relation:

@new_orders = Order.where(:status => 1).all

Upvotes: 3

Related Questions