Last active
December 29, 2015 11:49
-
-
Save laspluviosillas/7666699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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