2

I have two fields namely cost and var_cost both of type Integer, User can enter a number in the text field, on submit the number will be updated for the cost field for multiple records, but var_cost of the individual record should be added to the number before assigning it to the cost field.

Variation.where(conditions).update_all(cost: number + var_cost)

I have tried this but it is not working, is there a way to use var_cost of individual record with update_all, any help would be highly appreciated!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • The duplicate question doesn't have solution to the question I asked, please reopen – Rajdeep Singh Jan 06 '15 at 10:14
  • 2
    I think this is a duplicate of http://stackoverflow.com/q/3974985/479863 though. I think you're stuck doing it by hand document by document. You can send some JavaScript into MongoDB to do a `forEach` on the collection but it'll still be nasty. Mongoid isn't going to be able to let you do things that MongoDB doesn't support. – mu is too short Jan 07 '15 at 04:54

1 Answers1

0

The problem your having above is that rails is interpreting both number and var_cost. You want number to be interpreted by rails, and var_cost to be interpreted by your database.

Variation.where(conditions).update_all("cost = var_cost + #{number.to_i}")

Be sure if you don't do number.to_i that you sanitize it.

Camway
  • 1,010
  • 14
  • 23