Class: ActsAsTaggableSimple::Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActsAsTaggableSimple::Tag
- Includes:
- Mongoid::Document
- Defined in:
- lib/mongoid/tag.rb,
lib/active_record/tag.rb
Overview
ActiveRecord model for storing a tag in the database.
Tags have only one attribute: name
, which is just the text representation of the tag. name
must be unique for each instance.
Class Method Summary collapse
- .find_by_names(names) ⇒ Object
-
.find_or_create_all_tags(list) ⇒ Object
Finds or creates all of the tags contained in
list
and returns them.
Class Method Details
.find_by_names(names) ⇒ Object
40 41 42 |
# File 'lib/mongoid/tag.rb', line 40 def self.find_by_names(names) where(:name.in => names) end |
.find_or_create_all_tags(list) ⇒ Object
Finds or creates all of the tags contained in list
and returns them.
list
is an Array of String’s containing the name
‘s of the desired tags. If an ActsAsTaggableSimple::Tag does not yet exist for a tag, then it is created, otherwise the already existing tag is used.
Empty database example:
= ActsAsTaggableSimple::Tag.(%w{rails css html})
Will create 3 new ActsAsTaggableSimple::Tag objects and save them to the database.
If rails
, css
, and html
tags already exist in the database, then the following example will only create 1 new tag.
= ActsAsTaggableSimple::Tag.(%w{rails css html javascript})
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mongoid/tag.rb', line 29 def self.(list) = Tag.where(:name.in => list) = list - .map(&:name) = .collect do |tag| Tag.create!(:name => tag) end + .all end |