I m在铁路码头上工作,包括一种方式,通过协会修改相关记录的顺序。 我们有以下模式:
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
如果我们改变游览的顺序(例如@playlist.songs.rotate!
), 铁路没有触及游艇上的记录,因此具有意义。 我愿向歌唱歌唱团发出任何呼吁,以拯救歌唱的顺序,但也许可以删除歌剧中现有的相关行号,并按适当的顺序创建新行(<代码>:order =>“id”可在检索时加以使用),或者增加一个:打字栏,以 play歌唱歌,并相应地更新这些价值观。
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?