Saving the order of associated records in a Rails has_many :through association

試著忘記壹切 提交于 2019-12-03 14:07:09

Have you looked at acts_as_list? It's one of the most old-school of rails plugins, and is intended to handle this sort of problem.

Rather than sorting on id, it sorts on a positional column. Then it's simply a matter of updating the position, rather than the messy business of changing the id or deleting/replacing records.

In your case, you'd simply add a position integer column to PlayListSong, then:

class PlayListSong
  acts_as_list :scope => :play_list_id
end

As you point out in the comments, the methods in acts_as_list work mostly on individual items in the list, and there's no "reorder" functionality out of the box. I would not recommend tampering with replace_records to do this. It would be cleaner and more explicit to write a method making use of the same position column as the plugin. For example.

class PlayList
  # It makes sense for these methods to be on the association.  You might make it
  # work for #songs instead (as in your question), but the join table is what's
  # keeping the position.
  has_many :play_list_songs, ... do

    # I'm not sure what rotate! should do, so...

    # This first method makes use of acts_as_list's functionality
    #
    # This should take the last song and move it to the first, incrementing 
    # the position of all other songs, effectively rotating the list forward 
    # by 1 song.
    def rotate!
      last.move_to_top unless empty?
    end

    # this, on the other hand, would reorder given an array of play_list_songs.
    # 
    # Note: this is a rough (untested) idea and could/should be reworked for 
    # efficiency and safety. 
    def reorder!(reordered_songs)
      position = 0
      reordered_songs.each do |song|
        position += 1

        # Note: update_column is 3.1+, but I'm assuming you're using it, since
        # that was the source you linked to in your question
        find(song.id).update_column(:position, position)
      end
    end
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!