Rails Recipes: Drag'n'Drop Sortable List with unique index?

Hi all

On page 31 of the “Pragmatic Programmer’s Rails Recipes” book there’s
the recipe “Creating a Drag-and-Drop Sortable List”. I think this is
very useful, but sadly I have a unique constraint on my product_id and
position fields (because the acts_as_list scope is product_id).
When I try the recipe then Rails crashes very very badly: I have to
restart the server on a new port because the previously used port
seems to be locked or something like that (I’m using Locomotive)… No
idea.

So can anybody please help me rewriting the sort action so it is capable
to sort the stuff with a unique index on the fields product_id and
position? Thank you very much, I’m still no great Ruby programmer yet…
:slight_smile:

def sort
produkt = Produkt.find(params[:id])
produkt.bilder.each do |bild|
bild.position = params[‘bilder_list’].index(bild.id.to_s) + 1
bild.save
end
render :nothing => true
end

Keep care,
Josh