# Acts\_as\_taggable magic.... please explain

**URL:** <https://www.ruby-forum.com/t/acts-as-taggable-magic-please-explain/58342>\
**Category:** Rails\
**Created:** [April 20, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-magic-please-explain/58342 "2006-04-20T18:41:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 20, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-magic-please-explain/58342/1 "2006-04-20T18:41:20Z")

</div>

Hi,

There is a line in acts\_as\_taggable plugin:  
send(acts\_as\_taggable\_options[:from]).tags.find\_or\_create\_by\_name(name).on(self)

could someone explain what is:  
send(xxx)  
and  
.on(self)

I cannot find it rDoc

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 20, 2006, 8:10pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-magic-please-explain/58342/2 "2006-04-20T20:10:32Z")

</div>

Hi,

> send(acts\_as\_taggable\_options[:from]).tags.find\_or\_create\_by\_name(name).on(self)  
> could someone explain what is:  
> send(xxx)

“Send” sends a message to an object.  
So |“|Wiktor Schmidt|”.send(:length) if equivalent to “|Wiktor  
Schmidt|”.||length

In the case below, the :from option lets you |attribute the tag to  
another object.

Class Photo  
acts\_as\_taggable :from =\> :current\_user  
end

So the code above that you were asking about will then call  
Photo.current\_user.tags.find … instead of Photo.tags.find …  
Check out this chapter in the Pickaxe for some more info on send.  
[http://www.rubycentral.com/book/ospace.html](http://www.rubycentral.com/book/ospace.html)

> .on(self)  
> The on method is part of acts as taggable. You can find it in tag.rb

def on(taggable)  
taggings.create :taggable =\> taggable  
end

So if we simply it a bit:

#With the current user, find or create a tag called “mynewtag” and  
associate it with Photo  
#Actual Code →   
send(acts\_as\_taggable\_options[:from]).tags.find\_or\_create\_by\_name(name).on(self)  
#Code with hardcoded values  
Photo.current\_user.tags.find\_or\_create\_by\_name(“mynewtag”).on(Photo)

You can check out this post on Rails Wheenie for some extra help.  
[http://rails.techno-weenie.net/question/2006/3/6/how\_to\_add\_user\_to\_acts\_as\_taggable](http://rails.techno-weenie.net/question/2006/3/6/how_to_add_user_to_acts_as_taggable)

Hopefully this helps out a bit.

Cheers,  
Eric G.

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![iktorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b487fb/32.png) [@iktorn](https://www.ruby-forum.com/u/iktorn)\
**Post date:** [April 20, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/acts-as-taggable-magic-please-explain/58342/3 "2006-04-20T20:35:15Z")

</div>

“send” is a method available on all objects you can use to call an  
arbitrary method. object.send(:method, param) in Ruby is like object  
‘method’ in Javascript.

Used in this context, acts\_as\_taggable\_options[:from] contains a  
symbol for an association that responds to :tags. This returns the  
an association proxy of tags for that object, limiting the tags you  
create to a given tag space.

‘find\_or\_create\_by\_name’ will call the class method delegated from  
the ‘tags’ association. This returns a single Tag instance.

‘on’ is a method on the Tag instance that will create the join model  
to make the association between your model and the new/reused tag.

This roundabout way of creating the join model is needed because of  
the polymorphic association. For an explanation, take a look at:  
[http://blog.hasmanythrough.com/articles/2006/04/17/join-models-not-](http://blog.hasmanythrough.com/articles/2006/04/17/join-models-not-)  
proxy-collections

HTH,  
Sean
