![](/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.