If you’re really up for this, you can try a Unique index. see here for more: http://robots.thoughtbot.com/the-perils-of-uniqueness-validations

Albert


On Monday, April 14, 2014 at 5:05 PM, John Morgan wrote:

> In app/controllers/queue_items_controller.rb:
> > end > end > > + def all_queue_positions_valid?(new_queue_values) > + (all_queue_positions_integers?(new_queue_values) && > + all_queue_positions_unique?(new_queue_values)) > + end > + > + def all_queue_positions_integers?(new_queue_values)  
> I should take out the model validation for integers.  
> I found I couldn't do "validates_uniqueness_of :position" at the model level because in the middle of switching positions around, some of them will collide. So the solution was to check the uniqueness of positions all at once, before making changes, in the controller. Then because I had this extra validation condition, the official solution with transactions got a little unwieldy (especially since the unique position check does not throw an exception). So I just did both checks up front in the controller.
> Thoughts on a better approach are most welcome!  
>  
> —
> Reply to this email directly or view it on GitHub (https://github.com/Nissl/myflix/pull/7/files#r11595239).
>  
>  
>