Order Tags Alphabetically By Default Using Acts As Taggable On
Asked Answered
S

1

15

I'm using acts-as-taggable-on to allow my NewsItem and MediaItem models to be tagged. By default, accessing the model's tags using either tag_list for an array of the tag names or directly via an association using tags for collection of ActsAsTaggableOn::Tags, results in the tags in an arbitrary order. I would like them to always be alphabetised. There is no mention in the documentation of setting up a default order, other than a way of maintaining the order the tags were created in (by using acts_as_ordered_taggable in the model).

Obviously I can order them every time I call tags using news_item.tags.order(:name), but I would like this to be the default behaviour throughout the application and don't want to duplicate the ordering wherever I need to use tags.

How can I set up my model so that its tags association always returns its tags in alphabetical order?

Schweiker answered 13/3, 2015 at 10:41 Comment(0)
C
7

I'm afraid that this kind of ordering cannot be done in the current version of acts-as-taggable-on gem. It is apparent from the source code that this gem indeed only supports ordering by IDs (preserves the order of creation) or no ordering at all.

There is a pull request open that would allow ordering by name or any other attribute but the activity of the gem development seems to be quite stale right now (the pull request has been open for over a year already). Perhaps you might try to ask about the status of this pull request there.

Cyruscyst answered 28/4, 2017 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.