# Accessing the session in a model

**URL:** <https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955>\
**Category:** Rails\
**Created:** [August 30, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955 "2006-08-30T16:39:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955/1 "2006-08-30T16:39:42Z")

</div>

Hey  
i want to acces the session in the model, i have 3 columns, name\_nl,  
name\_en, name\_fr, depending on what is in @session[:language], i need to  
select to correct field.

i have something like this for a product item  
id int(11) Nee auto\_increment  
name\_en varchar(100) utf8\_general\_ci Nee  
name\_fr varchar(100) utf8\_general\_ci Nee  
name\_nl varchar(100) utf8\_general\_ci Nee  
price decimal(10,2) Nee 0.00  
publish tinyint(1) Nee 0  
publish\_from date Ja 0000-00-00  
publish\_to date Ja 0000-00-00  
created\_at datetime Nee 0000-00-00 00:00:00  
created\_by varchar(250) utf8\_general\_ci Ja NULL  
updated\_at datetime Nee 0000-00-00 00:00:00  
updated\_by varchar(250) utf8\_general\_ci Ja NULL  
deleted\_at datetime Ja 0000-00-00 00:00:00  
Selecteer alles / Deselecteer alles Met geselecteerd:

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955/2 "2006-08-30T16:46:39Z")

</div>

Sorry, pc got stuck here is the full message

Hey  
i want to acces the session in the model, i have 3 columns, name\_nl,  
name\_en, name\_fr, depending on what is in @session[:language], i need to  
select to correct field.

i have something like this for a product item  
id int(11) Nee auto\_increment  
name\_en varchar(100) utf8\_general\_ci Nee  
name\_fr varchar(100) utf8\_general\_ci Nee  
name\_nl varchar(100) utf8\_general\_ci Nee  
price decimal(10,2) Nee 0.00  
publish tinyint(1) Nee 0  
publish\_from date Ja 0000-00-00  
publish\_to date Ja 0000-00-00  
created\_at datetime Nee 0000-00-00 00:00:00  
created\_by varchar(250) utf8\_general\_ci Ja NULL  
updated\_at datetime Nee 0000-00-00 00:00:00  
updated\_by varchar(250) utf8\_general\_ci Ja NULL  
deleted\_at datetime Ja 0000-00-00 00:00:00

this is my code to fetch the name field

class Product \< ActiveRecord::Base  
def name  
case @session[:language]  
when “nl”  
return self.name\_nl  
when “en”  
return self.name\_en  
when “fr”  
return self.name\_fr  
end  
end  
end

* * *

## i can always use something like this as datastructure product:

## id int(11) Nee auto\_increment price decimal(10,2) Nee 0.00 publish tinyint(1) Nee 0 publish\_from date Ja 0000-00-00 publish\_to date Ja 0000-00-00 created\_at datetime Nee 0000-00-00 00:00:00 created\_by varchar(250) utf8\_general\_ci Ja NULL updated\_at datetime Nee 0000-00-00 00:00:00 updated\_by varchar(250) utf8\_general\_ci Ja NULL deleted\_at datetime Ja 0000-00-00 00:00:00 productlang:

id int(11) Nee auto\_increment  
name varchar(100) utf8\_general\_ci Nee  
lang varchar(4) utf8\_general\_ci Nee  
parent int(11) Nee 0

saving the language depended fields in another table, but then i need to  
use encapsulation. and i have tried that before, but then there was an  
update of rails and it didnt work anymore. So i want to use something  
good instead.

Anyone has an good idea? Or can help me with this?

I need also validation on the virtual field “name”. Not on name\_fr,  
name\_nl, name\_en cuz these can be empty.  
Like name =\> length max 100 chars =\> then the value will be copied  
(before saving) to the correct field (depending on @session[:language])

Thx

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955/3 "2006-08-30T16:49:58Z")

</div>

Or in stead of:

case @session[:language]

i want

case getFrontendLanguage()

This in the application controller:

def getFrontendLanguage()  
if @session[:language].nil? || @session[:language].blank?  
@session[:language] = getFrontendDefaultLanguage()  
end  
return @session[:language]  
end

def getFrontendDefaultLanguage()  
doc = REXML::Document.new File.new(“config/project.xml”)  
doc.elements.each("\*/languages/frontend") { |element|  
return element.attributes[“default\_lang”]  
}  
end

## project.xml:

thx

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955/4 "2006-08-30T17:25:25Z")

</div>

Hi Nick,

Nick B. wrote:

> case @session[:language]

First thing to do is stop using ‘@session[]’. It’s use has been  
depracated.  
Use ‘session[]’ instead. You’ll have problems otherwise.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955/5 "2006-08-30T18:18:18Z")

</div>

Not sure exactly what’s the best way to do it, but I would think about  
creating a virtual field for your model that contains the language value  
you need. Pass the session variable into your model’s virtual field  
when creating or updating an object.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [August 30, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/accessing-the-session-in-a-model/66955/6 "2006-08-30T18:49:40Z")

</div>

Avoid session use in the model. It’s ill advised and goes against MVC.  
You  
may someday want to use your models in a CRON job or an external process  
that does not require the web. Sessions are a controller piece.

Try this instead:

class Product \< ActiveRecord::Base  
attr\_accessor :language  
def name  
case self.language  
when “nl”  
return self.name\_nl  
when “en”  
return self.name\_en  
when “fr”  
return self.name\_fr  
end  
end  
end

@product = Product.find 1  
@product.language = session[:language]  
@product.name

if @product.language = session[:language] gets to be too cumbersome to  
type, wrap it in a method you call

In application.rb add this:

# finds the product and sets the language type based on session contents

# product id defaults to params[:id] if no product id is provided.

def get\_product(id = params[:id])  
product = Product.find 1  
product.language = session[:language]  
product  
end

Now you just do

def show  
@product = get\_product  
end

This gets more complicated in collections though

@products = Product.find :all  
@products.each do |product|  
product.language = session[:language]  
end

But you could wrap that too.

Hope that helps you out. Looks like you’re doing internationalization.  
You  
might look into some plugins for that. I’ve not dealt with that yet but  
I  
know others are. Why not ask the list for some advice on the problem  
you’re  
trying to solve as well… I know others would be interested in the  
response.

Have a nice day!
