# Access to session from plugins

**URL:** <https://www.ruby-forum.com/t/access-to-session-from-plugins/72393>\
**Category:** Rails\
**Created:** [February 23, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393 "2006-02-23T17:18:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias\_StjernstrSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c67d28/32.png) [@Mathias\_StjernstrSS](https://www.ruby-forum.com/u/Mathias_StjernstrSS)\
**Post date:** [February 23, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393/1 "2006-02-23T17:18:07Z")

</div>

Hi!  
I am writing an acts\_as\_ plugin and i need to access session data  
from it. But i cant find any information if this is possible? And if  
it is, how.

Cheers!

Mathias

---

<div class="post-metadata">

**Author:** ![Mathias\_StjernstrSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c67d28/32.png) [@Mathias\_StjernstrSS](https://www.ruby-forum.com/u/Mathias_StjernstrSS)\
**Post date:** [February 23, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393/2 "2006-02-23T18:09:45Z")

</div>

Am 23.02.2006 um 17:17 schrieb Mathias  
Stjernström:

> Hi!  
> I am writing an acts\_as\_ plugin and i need to access session data  
> from it. But i cant find any information if this is possible? And  
> if it is, how.

Create a Singleton class and set the session into it in your  
ApplicationController’s before\_filter.

For example:

class SessionFoomatic  
def self.session=(value)  
@@session = value  
end

def self.session  
@@session  
end  
end

class ApplicationController \< ActionController::Base

before\_filter :session\_foo

protected

def session\_foo  
SessionFoomatic.session = session  
end  
end

\*m\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![Mathias\_StjernstrSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c67d28/32.png) [@Mathias\_StjernstrSS](https://www.ruby-forum.com/u/Mathias_StjernstrSS)\
**Post date:** [February 23, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393/3 "2006-02-23T21:54:47Z")

</div>

Ah, works like a charm, thanks!

Another quite annoying thing is that i have to reload my webserver  
after every modification that i make in my plugin, are there any way  
of have it auto-reloaded?

Thanks again for the example!

Cheers!

Mathias

---

<div class="post-metadata">

**Author:** ![Mathias\_StjernstrSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c67d28/32.png) [@Mathias\_StjernstrSS](https://www.ruby-forum.com/u/Mathias_StjernstrSS)\
**Post date:** [February 23, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393/4 "2006-02-23T22:25:22Z")

</div>

I think this is necessary because of the way plugins are loaded.  
I don’t know if there is a way around it, or if it would be a good idea  
if you could.

\_Kevin

---

<div class="post-metadata">

**Author:** ![Mathias\_StjernstrSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c67d28/32.png) [@Mathias\_StjernstrSS](https://www.ruby-forum.com/u/Mathias_StjernstrSS)\
**Post date:** [February 25, 2006, 1:30pm UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393/5 "2006-02-25T13:30:53Z")

</div>

I have no special injections in my init.rb but maybe i should 😉

I found that calling require\_dependency ‘acts\_as\_myplugin’ in the  
controller reloads the plugin.  
Do not know if this breaks anything but its only when developing the  
plugin and so far everything seem good 😉

Cheers!

Mathias

---

<div class="post-metadata">

**Author:** ![Mathias\_StjernstrSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c67d28/32.png) [@Mathias\_StjernstrSS](https://www.ruby-forum.com/u/Mathias_StjernstrSS)\
**Post date:** [February 25, 2006, 3:54am UTC](https://www.ruby-forum.com/t/access-to-session-from-plugins/72393/6 "2006-02-25T03:54:10Z")

</div>

It depends on your particular plugin. Your init.rb file will only be  
evaluated once (during the Initializer processing that gets kicked off  
when environment.rb is loaded). As i recall, the ‘acts\_as\_’ plugin  
pattern uses some ActiveRecord injection cunning that happens in the  
init.rb file, so this might be an issue wrt. reloading.

- james

On 2/23/06, Mathias Stjernström [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mathias
> 
> > > from it. But i cant find any information if this is possible? And  
> > > end  
> > > protected  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

- J \*  
~
