I have a fairly large question that I've been unable to resolve after a full day of reading articles, documentation and other Stack questions. At this point it's just messing with my mind!
I have an app that is Vue/Vuetify on the frontend with a data-table that I want to be able to reorder with sortablejs. On the backend, I have a Rails API that I'd like to use the acts_as_list gem to handle reordering for me.
Ideally I'd be able to call something like Category.first.items.last.move_higher
, however because I have a join model I'm having to put acts_as_list
and the position column on the join model instead of the Item model. Is there a better way to arrange this?
category.rb
class Category < ApplicationRecord
has_many :categories_items
has_many :items, through: :categories_items, source: :item
accepts_nested_attributes_for :items
end
item.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
With that structure you'll definitely need to reorder by calling methods on the join model. You can however use the Rails delegate
class method to delegate reordering methods to the join model if you like:
https://api.rubyonrails.org/classes/Module.html#method-i-delegate
All of this assumes that you need items
and categories
to have a has_and_belongs_to_many
type relationship.