# Passing model data to a plugin called within that model

**URL:** <https://www.ruby-forum.com/t/passing-model-data-to-a-plugin-called-within-that-model/177112>\
**Category:** Rails\
**Created:** [October 11, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/passing-model-data-to-a-plugin-called-within-that-model/177112 "2009-10-11T23:36:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 11, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/passing-model-data-to-a-plugin-called-within-that-model/177112/1 "2009-10-11T23:36:49Z")

</div>

I am making a plugin which extends ActiveRecord::Base

I have the plugin working in my models in other aspects. I would like  
to pass in some data to the plugin, but I can’t get the data I would  
like.

so if I have:

class Stuff \< ActiveRecord::Base  
belongs\_to :thing

my\_plugin :id =\> self.thing.other\_id

end

it chokes on figuring out what to do with self.thing.other\_id.

I checked, and self isn’t recognized at the point that I am calling  
my\_plugin

so if i do :

class Stuff \< ActiveRecord::Base  
belongs\_to :thing

my\_plugin :id =\> self.class.to\_s

end

when I print out :id the class result comes back as blank.

Is there a way to do this?  
The closest I can get is I can sort of do self.send(:thing).send  
(:other\_id)  
It didn’t 100% work, but it was starting to get clunky, and I didn’t  
pursue the error I was creating.  
There has to be something obvious I am missing.

thanks,  
Charles

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 12, 2009, 3:54am UTC](https://www.ruby-forum.com/t/passing-model-data-to-a-plugin-called-within-that-model/177112/2 "2009-10-12T03:54:15Z")

</div>

> class Stuff \< ActiveRecord::Base  
> belongs\_to :thing
> 
> my\_plugin :id =\> self.thing.other\_id
> 
> end

This is impossible. self in the above snippet is the Stuff class. I’m  
assuming you want that to use a instance of Stuff at the time some  
magical method is called. You have to use a block

class Stuff \< ActiveRecord::Base  
my\_plugin do |stuff|  
{:id =\> stuff.thing.other\_id}  
end  
end

The my\_plugin method has to know how to store that block so that the  
instance can call it, passing itself as the first argument.
