Tom*_*Tom 7 activerecord ruby-on-rails associations has-many-through ruby-on-rails-plugins
我正在开发一个Rails插件,其中包含一种修改has_many中关联记录顺序的方法:通过关联.假设我们有以下型号:
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
Run Code Online (Sandbox Code Playgroud)
如果我们改变播放列表歌曲的顺序(例如@playlist.songs.rotate!),Rails不会触及playlists_songs表中的记录(我正在使用Rails 3.1),这是有道理的.我想调用播放列表的歌曲=方法保存歌曲的顺序,但是,可以通过删除播放列表中的相关现有行并以正确的顺序创建新的行(这样:order => "id"可以在检索它们时使用)或者通过向playlists_songs添加sort:integer列并相应地更新这些值.
我没有看到任何允许这样做的回调(例如before_add).在ActiveRecord :: Associations :: CollectionAssociation中,相关的方法似乎是writer,replace和replace_records,但我对最好的下一步将会失败.有没有办法扩展或安全地覆盖这些方法之一,以允许我正在寻找的功能(最好只针对特定的关联),或者是否有一个不同的,更好的方法呢?
你看过acts_as_list吗?它是最老派的rails插件之一,旨在解决这类问题.
它不是排序id,而是对位置列进行排序.然后,这只是更新位置的问题,而不是更改id或删除/替换记录的混乱业务.
在您的情况下,您只需添加一个position整数列PlayListSong,然后:
class PlayListSong
acts_as_list :scope => :play_list_id
end
Run Code Online (Sandbox Code Playgroud)
正如您在评论中指出的那样,这些方法acts_as_list主要针对列表中的各个项目,并且没有开箱即用的"重新排序"功能.我不建议篡改replace_records这样做.编写一个使用与插件相同的位置列的方法会更清晰,更明确.例如.
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3964 次 |
| 最近记录: |