0

我想实现播放列表中项目列表的拖放。播放列表内容具有不同的活动记录类型。我需要存储位置并将其保存在数据库中并使用acts_as_list 处理排序。我不确定我是否正确地建立了关联。

我使用连接表建立了has_many_through关系,但我不确定如何让acts_as_list 使用此配置。

我的模型是这样的:

class Playlist < ActiveRecord::Base

  belongs_to :group
  belongs_to :user

  has_many :links, through: :playlists_contents dependent: :destroy
  has_many :medias,  through: :playlists_contents dependent: :destroy
end

我对连接表的迁移如下所示:

class CreateJoinTable < ActiveRecord::Migration[5.0]
  def change
    create_table :contents_playlists do |t|
      t.belongs_to :link, index: true
      t.belongs_to :media, index: true
      t.belongs_to :playlist, index: true
    end
  end

到目前为止,我的连接表模型中有这个:

class PlaylistsContents < ActiveRecord::Base

  default_scope -> { order(position: :asc) }

  default_scope :order => 'position'
  belongs_to :playlist
  belongs_to :link
  belongs_to :media
  acts_as_list :scope => :link
  acts_as_list :scope => :media

end
4

1 回答 1

1

您需要positioncontents_playlists(或playlists_contents- 因为模型称为 PlaylistsContents?)表中的字段。您可以使用以下方法添加多个范围:

acts_as_list scope: [:playlist, :link, :media]

编辑:添加:playlist

于 2018-11-21T08:59:34.880 回答