Given the following models:
class Menu < ActiveRecord::Base
has_many :items
end
class Items < ActiveRecord::Base
has_and_belongs_to_many :menus
end
Each menu needs to have a separate sort order for it’s items
I’d normally add a sort_order column on the join table menus_items
Do I need to create a new model to store this info and add a :through relationship?
What’s the best way to set this up in Rails?
Yep, you’ll need to create a new
MenuItemclass and associated menu_items table.Your menu_items table should have a column for
menu_id,item_id, andsort_order(or whatever you are using as the name of your sorting column). Remember though, you need to handle the setting ofsort_order. You could use the acts_as_list plugin or roll in the logic yourself, depending on your requirements.