在阅读文章、文档和其他 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