Skip to content

Instantly share code, notes, and snippets.

@laspluviosillas
Last active December 29, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laspluviosillas/7666699 to your computer and use it in GitHub Desktop.
Save laspluviosillas/7666699 to your computer and use it in GitHub Desktop.
def update
# fetch @conversation having its rank updated.
#
conversations = @discussion.conversations.order(:rank).to_a
# Move conversation to new position by inserting in array at rank and deleting it from previous position.
conversations.insert(@conversation.rank, conversations.delete_at(@conversation))
# Update all conversation ranks to correspond to new array order.
# Very inefficient as this uses a n+1 query. You could optimize to use update_all
# or something of the sort.
conversations.each_with_index do |c, i|
c.update_attributes(rank: i)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment