Home > Enterprise >  Best practice for user-sorted has_many associations in Rails?
Best practice for user-sorted has_many associations in Rails?

Time:11-09

I have several models where my users need to be able to sort some has_many associations. That is, they need to be able to add, remove and prioritise certain relationships.

So far, my idea is to have an order property on the associated models, and handle updating them from the frontend.

However, I feel like this is prone to many errors. On the other hand, I also feel like introducing an intermediary table/model would probably be an overkill.

Is there a best practice to handle this (not so uncommon) situation?

CodePudding user response:

I would say that adding the order column is the standard way. There is a fairly popular gem called acts_as_list that does it that way. I've used that gem and didn't have any issues, but it was in an admin app (very low chance of concurrency issues).

  • Related