Reputation: 5693
I am trying to dynamically sort a table of Posts
by :type
using acts-as-taggable-on gem.
Let's say my Post object has 3 columns: user, title and date. I can easily sort a table of Post by putting in my controller @entries = Post.order(sort_column + " " + sort_direction).page(params[:page])
sort_column
and sort_direction
being referred as:
def sort_column
Post.column_names.include?(params[:sort]) ? params[:sort] : "published_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
However, Post can also have a type attached to it. In my Post model, I have:
acts_as_taggable
acts_as_taggable_on :types
and in the table of my view I simply display the tag by writing:
views/posts/_posts.html.slim
...
td.tags = entry.type_list.first.titleize unless entry.type_list.blank?
...
Is there an easy way to alphabetically sort the table by type?
Upvotes: 3
Views: 577
Reputation: 8122
The acts-as-taggable-on
gem adds polymorphic association. You can take full advantage of Active record.
You have to do something like this. This will give your post
in alphabetical
order.
Post.includes(:taggings).joins(:types).order("tags.name")
Hope this help.
Upvotes: 2