-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Finding Tagged Objects
Abdelkader Boudih edited this page Apr 2, 2014
·
2 revisions
Acts As Taggable On uses scopes to create an association for tags. This way you can mix and match to filter down your results.
class User < ActiveRecord::Base
acts_as_taggable_on :tags, :skills
scope :by_join_date, order("created_at DESC")
end
User.tagged_with("awesome").by_join_date
User.tagged_with("awesome").by_join_date.paginate(:page => params[:page], :per_page => 20)
# Find a user with matching all tags, not just one
User.tagged_with(["awesome", "cool"], :match_all => true)
# Find a user with any of the tags:
User.tagged_with(["awesome", "cool"], :any => true)
# Find a user that not tags with awesome or cool:
User.tagged_with(["awesome", "cool"], :exclude => true)
# Find a user with any of tags based on context:
User.tagged_with(['awesome', 'cool'], :on => :tags, :any => true)
.tagged_with(['smart', 'shy'], :on => :skills, :any => true)
You can also use :wild => true
option along with :any
or :exclude
option. It will looking for %awesome%
and %cool%
in sql.
Tip: User.tagged_with([])
or User.tagged_with('')
will return []
, but not all records.