Is there a way to atomically exchange values of records which are
Some details follow.
Imagine I have a model:
class Item < ActiveRecord::Base
‘weight’ is a sorting weight.
I have an index.html view which has a list of “Item” records wrapped in
jQueryUI’s sortable container.
My goal is to do some ajax request (POST i guess) whenever user changes
items’ order. I.e. i plan to do end up with controller action that calls
def self.exchange_weights(id1, id2)
item1 = Item.find(id1)
item2 = Item.find(id2)
weight1 = Item.find(id1).weight
weight2 = Item.find(id2).weight
item1.weight = some_temp_weight
item2.weight = weight1
item1.weight = weight2
Is this the right way to do this?
Or can I somehow just do a separate POST requests on these Item’s
by triggering ItemsController#update for each of them and passing weight
but here validations would not allow me to do this unless I miss
- that’s why I desided to ask