Acts-As-Taggable-On Find All Tags by Context

Get all tags by context for acts-as-taggable-on

This ended up generating MUCH more efficient queries:

Tagging.where(:context => context).joins(:tag).select('DISTINCT tags.name').map{ |x| x.name}

How to get *ALL* tags on an acts_as_taggable object regardless of its contexts

c.base_tags.map(&:name)

There is no built-in shortcut to get the names directly, but that's short enough :)

Edit: base_tags is an association defined on classes declared as taggable: has_many :base_tags, through: :taggings, source: :tag, class_name: '::ActsAsTaggableOn::Tag'

Source: https://github.com/mbleigh/acts-as-taggable-on/blob/master/lib/acts_as_taggable_on/taggable.rb

How to get a list of all tags while using the gem 'acts-as-taggable-on' in Rails (not the counts)

The tags are stored in the Tags table, which you access from your program with e.g.

ActsAsTaggableOn::Tag.all

If you need more info on tag usage, there is also the table

ActsAsTaggableOn::Tagging

which contains links to where each tag is being used, including its context

To further apply this to your example, you can use

ActsAsTaggableOn::Tagging.includes(:tag).where(context: 'deshanatags').map { |tagging| { 'id' => tagging.tag_id.to_s, 'name' => tagging.tag.name } }.uniq

Let's explain the various parts in this statement:

  • the 'includes' makes sure the different tags are "eager loaded", in other words, that instead of loading n+1 records, only 2 queries will be done on the database
  • the 'where' limits the records to the ones with the given context
  • the 'map' converts the resulting array of records into a new array, containing the hashes you asked for in your problem, with id mapped to the tag's id, and name mapped to the tag's name
  • finally the 'uniq' makes sure that you don't have doubles in your list

To also cope with your additional problem, being taggins without tag, you could extend the where clause to

where("(context = 'deshanatags') AND (tag_id IS NOT NULL)")

Acts-as-taggable-on find all tags by context

You could also use a statement like the following:

# Returns all the tags for the specified model/context with a count >= 1
@tags = YourModel.tag_counts_on(**context**)

Add limit and order:

# Get the top 5 tags by count
@tags = YourModel.tag_counts_on(**context**, :limit => 5, :order => "count desc")

Access the counts with the count attribute of the tags returned from tag_counts_on

tag.count

Return all tags based on context - ActsAsTaggableOn

I have never used acts-as-taggable-on but a quick browse through of the code suggests, you can do:

# to get all the tags with context topic with counts
ActsAsTaggableOn::Tagging.
includes(:tag).
where(:context => "topics").
group("tags.name").
select("tags.name, COUNT(*) as count")

You should probably take a look at ActsAsTaggableOn::Tagging, ActsAsTaggableOn::Tag and the migration file in your db/migrations folder to figure out how you can go about it.

If you don't want the count, only the tag names:

tags = ActsAsTaggableOn::Tag.includes(:taggings).
where("taggings.context = 'topics'").
select("DISTINCT tags.*")

# usage
tags.each {|tag| puts tag.name}

I hope that answers your question.

How to search by context using acts-as-taggable-on

Use the on option:

User.tagged_with(["1"], :on => :skills)

See the section entitled "Dynamic Tag Contexts" at the readme and this cheat sheet for a reference.

Rails Acts as Taggable On--Multiple Context Tag filter

The @selected_tag is tags that have been selected as parameter in index page.

Controller

def index
if params[:keyword].present?
@selected_tag = params[:keyword]
@cars = Car.tagged_with(params[:keyword])
else
@cars = Car.all.order(:cached_weighted_score => :desc)
end
end

View

It's only prototype view so you have to combine with your own code.

<% @tags.each do |tag| %>
<div>
<%= check_box_tag "keywords[]", tag.name, @selected_tag.present? ? @selected_tag.include?(tag.name) : '' %>
<%= tag.name %>
</div>
<% end %>

I hope I can give a general overview with my description.
For more information to prepopulate data in checkbox

Get all tags on taggable object - no matter context

You table with all tags is

ActsAsTaggableOn::Tagging

To get all tags you do this

ActsAsTaggableOn::Tag.all

how to find entries with no tags using acts-as-taggable-on?

Without knowing the internals of acts-as-taggable-on I can't think of a neat way that doesn't involve looping queries or raw SQL. This is readable:

need_to_tag = []

Entry.all.each do |e|
need_to_tag << e if e.tag_list.blank?
end

need_to_tag then holds any untagged Entries.



Related Topics



Leave a reply



Submit