![](/img/trans.png)
[英]Rails: Show all associated records on a has_many through association
[英]Saving the order of associated records in a Rails has_many :through association
我正在开发一个Rails插件,该插件包括一种修改has_many:through关联中关联记录顺序的方法。 假设我们有以下模型:
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!
),Rails不会触摸playlists_songs表中的记录(我使用的是Rails 3.1),这很有意义。 我想对Playlist的songs =方法进行任何调用,以保存Songs的顺序,也许是通过删除playlists_songs中现有的相关行并以适当的顺序创建新行(以便:order => "id"
可以在检索它们时使用)或通过在playlists_songs中添加sort:integer列并相应地更新这些值。
我没有看到任何允许这样做的回调(例如before_add)。 在ActiveRecord :: Associations :: CollectionAssociation中 ,相关的方法似乎是writer , replace和replace_records ,但是我对下一步的最佳方法迷失了。 有没有一种方法可以扩展或安全地覆盖这些方法中的一种,以允许我正在寻找的功能(最好仅针对特定的关联),或者是否有其他更好的方法呢?
您看过act_as_list吗? 它是最古老的Rails插件之一,旨在解决此类问题。
而不是对id
排序,而是对位置列进行排序。 然后,仅需更新位置,而不是更改id
或删除/替换记录的麻烦事务。
在您的情况下,您只需将position
整数列添加到PlayListSong
,然后:
class PlayListSong
acts_as_list :scope => :play_list_id
end
正如您在注释中指出的那样, acts_as_list
的方法主要对列表中的单个项目起作用,并且没有开箱即用的“重新排序”功能。 我不建议篡改replace_records
来做到这一点。 编写一种使用与插件相同的position列的方法会更加简洁明了。 例如。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.