Hi fellows!
Is there a way to atomically exchange values of records which are
validated
using ‘validates_uniqueness_of’?
Some details follow.
Imagine I have a model:
class Item < ActiveRecord::Base
validates_uniqueness_of :weight
end
‘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
function like:
class Item
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
end
end
Is this the right way to do this?
Or can I somehow just do a separate POST requests on these Item’s
instances
by triggering ItemsController#update for each of them and passing weight
but here validations would not allow me to do this unless I miss
something
- that’s why I desided to ask
Thanks.