# Access to session variable in model

**URL:** <https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390>\
**Category:** Rails\
**Created:** [September 30, 2009, 12:12am UTC](https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390 "2009-09-30T00:12:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [September 30, 2009, 12:12am UTC](https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390/1 "2009-09-30T00:12:18Z")

</div>

Hi everyone

I am using attachment fu for image uploading. I try to customize the  
path for pictures be saved.

If I call session[:kwikerhash] in the image.rb where there method for  
path definition is I get an undefined method or variable error. If I  
fill a global variable @kwikerhash in the controller with the session  
value and try to use it in the image.rb it says that @kwikerhash is nil.

Is there a possibility to get access to session values in model files?

Thanks in advance.

Regards

Adam

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [September 30, 2009, 6:51am UTC](https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390/2 "2009-09-30T06:51:08Z")

</div>

Hi Adam M.

> Is there a possibility to get access to session values in model files?

You can’t do that and if any means doing that means violating MVC .  
What you can do is to pass the session variable from controller to model  
For example in the image.rb you have a

## def do\_this(session\_id)

end

So from controller you can pass it like

@image\_model\_obj.do\_this(session[:kwikerhash])

Sijo

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [September 30, 2009, 12:38pm UTC](https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390/3 "2009-09-30T12:38:14Z")

</div>

Sijo Kg wrote:

> Hi Adam M.
> 
> > Is there a possibility to get access to session values in model files?
> 
> You can’t do that and if any means doing that means violating MVC .  
> What you can do is to pass the session variable from controller to model  
> For example in the image.rb you have a
> 
> ## def do\_this(session\_id)
> 
> end
> 
> So from controller you can pass it like
> 
> @image\_model\_obj.do\_this(session[:kwikerhash])
> 
> Sijo

Hi Sijo

you’re right. But if I set a value to @kwikerhash in the controller I  
have also access to it in the view.

This is fucked. Because its a method which I override from the  
attachment\_fu plugin.

So I need to create the same method with parameters.

Thanks

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [October 1, 2009, 1:00am UTC](https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390/4 "2009-10-01T01:00:21Z")

</div>

On your image model, you could add, for example:

class Image \< ActiveRecord::Base  
attr\_writer :session\_date  
…

and, on you controller, right after you create a new image:

@image = Image.new \<whatever\_parameters\>  
@image.session\_date = session[:kwikerhash][\<whatever\_param\>]

Hope this helps!

On Sep 30, 6:54 pm, Adam M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adammontgomery](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@adammontgomery](https://www.ruby-forum.com/u/adammontgomery)\
**Post date:** [September 30, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/access-to-session-variable-in-model/176390/5 "2009-09-30T23:54:20Z")

</div>

Hello again,

sorry but I have no Idea how to solve it. I try to explain the problem.  
I am using attachment\_fu for picture upload and output. I wanna change  
the attachment-fu to save the pics in a specific folder. Therefor my  
image.rb looks

class Image \< ActiveRecord::Base  
has\_many :categories  
belongs\_to :category  
belongs\_to :products  
belongs\_to :kwikers

has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system, :path\_prefix =\> ‘public/assets/uploads’,  
:max\_size =\> 10.megabytes,  
:resize\_to =\> ‘1024x1024\>’,  
:thumbnails =\> { :thumb =\> ‘150x150\>’},  
:processor =\> :MiniMagick # attachment\_fu looks in this order:  
ImageScience, Rmagick, MiniMagick  
validates\_as\_attachment

def uploaded\_data=(file\_data)  
return nil if file\_data.nil? || file\_data.size == 0  
self.content\_type = file\_data.content\_type  
extension = file\_data.original\_filename.slice(/.\w+$/)

```
 self.filename = Digest::SHA1.hexdigest(Time.now.to_s) + extension

 File.extname(file_data.original_filename) if respond_to?(:filename)
 if file_data.is_a?(StringIO)
   file_data.rewind
   self.temp_data = file_data.read
 else
     self.temp_path = file_data.path
 end

```

end

# START: FOLDER STRUCTURE MODIFYING CODE

# Changes the folder structure

def full\_filename(thumbnail = nil)  
file\_system\_path = (thumbnail ? thumbnail\_class :  
self).attachment\_options[:path\_prefix]  
File.join(RAILS\_ROOT, file\_system\_path,  
created\_at.strftime("%Y/%m/%d"), thumbnail\_name\_for(thumbnail))  
end  
end

I want to replace the created\_at.strftime("%Y/%m/%d") in the  
full\_filename method with the value which is set in  
session[:kwikerhash]. But I don’t know how. The view file calls a method  
image.public\_filename(:thumb) for example.

What can I do to let the full\_filename method take the value from the  
session?

Please help.

Adam
