I'm working on a Rails plugin that includes a way to modify the order of associated records in a has_many :through association. Say we have the following models:
class Playlist < ActiveRecord::Base
has_many :playlists_songs, :dependent => :destroy
has_many :songs, :through => :playlists_songs
end
class Song < ActiveRecord::Base
has_many :playlists_songs, :dependent => :destroy
has_many :playlists, :through => :playlists_songs
end
class PlaylistsSong < ActiveRecord::Base
belongs_to :playlist
belongs_to :song
end
If we change the order of a Playlist's Songs (e.g. @playlist.songs.rotate!
), Rails doesn't touch the records in the playlists_songs table (I'm using Rails 3.1), which makes sense. I'd like to make any call to Playlist's songs= method save the order of the Songs, though, perhaps by either deleting the relevant existing rows in playlists_songs and creating new ones in the proper order (so that :order => "id"
could be used when retrieving them) or by adding a sort:integer column to playlists_songs and updating those values accordingly.
I didn't see any callbacks (e.g. before_add) that would allow this. In ActiveRecord::Associations::CollectionAssociation, the relevant methods seem to be writer, replace, and replace_records, but I'm lost on what the best next step would be. Is there a way to extend or safely override one of these methods to allow for the functionality I'm seeking (preferably for only specific associations), or is there a different, better approach for this?