将关联记录的顺序保存在Rails has_many中:通过关联 [英] Saving the order of associated records in a Rails has_many :through association

查看:85
本文介绍了将关联记录的顺序保存在Rails has_many中:通过关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Rails插件,其中包括一种修改has_many:through关联中关联记录顺序的方法。假设我们有以下模型:

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

如果我们更改播放列表的歌曲的顺序(例如 @ playlist.songs.rotate!),Rails不会触摸playlists_songs表中的记录(使用Rails 3.1),这很有意义。我想对Playlist的songs =方法进行任何调用,以保存Songs的顺序,也许是通过删除playlists_songs中相关的现有行并以正确的顺序创建新行(以便: order => id 可以在检索它们时使用),或通过在playlists_songs中添加sort:integer列并相应地更新这些值。

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.

我没有看到任何允许这样做的回调(例如before_add)。在 ActiveRecord :: Associations :: CollectionAssociation 中,相关方法似乎是作家替换,并replace_records ,但是我迷失了下一步的最佳方法。是否有一种方法可以扩展或安全地覆盖这些方法之一,以允许我正在寻找的功能(最好仅针对特定的关联),或者有其他更好的方法吗?

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?

推荐答案

您是否看过 acts_as_list ?它是最古老的Rails插件之一,旨在解决此类问题。

Have you looked at acts_as_list? It's one of the most old-school of rails plugins, and is intended to handle this sort of problem.

它不是在 id 上排序,而是在位置列上排序。然后,只需要更新职位,而不是更改 id 或删除/替换记录的麻烦工作。

Rather than sorting on id, it sorts on a positional column. Then it's simply a matter of updating the position, rather than the messy business of changing the id or deleting/replacing records.

在您的情况下,您只需将位置整数列添加到 PlayListSong ,然后:

In your case, you'd simply add a position integer column to PlayListSong, then:

class PlayListSong
  acts_as_list :scope => :play_list_id
end

正如您在注释中指出的那样,<$ c中的方法$ c> acts_as_list 主要用于列表中的单个项目,并且没有开箱即用的重新排序功能。我不建议篡改 replace_records 来做到这一点。编写一种使用与插件相同的position列的方法会更加简洁明了。例如。

As you point out in the comments, the methods in acts_as_list work mostly on individual items in the list, and there's no "reorder" functionality out of the box. I would not recommend tampering with replace_records to do this. It would be cleaner and more explicit to write a method making use of the same position column as the plugin. For example.

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

这篇关于将关联记录的顺序保存在Rails has_many中:通过关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆