1

在阅读文章、文档和其他 Stack 问题一整天后,我有一个相当大的问题无法解决。在这一点上,它只是在扰乱我的想法!

我在前端有一个 Vue/Vuetify 应用程序,它带有一个数据表,我希望能够使用 sortablejs 重新排序。在后端,我有一个 Rails API,我想使用acts_as_list gem 来为我处理重新排序。

理想情况下,我可以调用类似的东西Category.first.items.last.move_higher,但是因为我有一个连接模型,我必须acts_as_list在连接模型而不是 Item 模型上放置位置列。有没有更好的方法来安排这个?

类别.rb

class Category < ApplicationRecord
  has_many :categories_items
  has_many :items, through: :categories_items, source: :item

  accepts_nested_attributes_for :items

end

项目.rb

class Item < ApplicationRecord
  has_many :categories_items
  has_many :categories, through: :categories_items, source: :category

end

categories_item.rb

class CategoriesItem < ApplicationRecord
  belongs_to :category
  belongs_to :item

  acts_as_list scope: :category
end
4

1 回答 1

0

使用这种结构,您肯定需要通过调用连接模型上的方法来重新排序。但是,如果您愿意,可以使用 Railsdelegate类方法将重新排序方法委托给连接模型:

https://api.rubyonrails.org/classes/Module.html#method-i-delegate

所有这些都假设您需要itemscategories具有has_and_belongs_to_many类型关系。

于 2020-03-25T05:51:57.170 回答