Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order Tags Alphabetically By Default Using Acts As Taggable On

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?

like image 539
Undistraction Avatar asked Mar 13 '15 10:03

Undistraction


1 Answers

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.

like image 163
Matouš Borák Avatar answered Nov 09 '22 13:11

Matouš Borák