# Multi language pages (access @session from a model)

**URL:** <https://www.ruby-forum.com/t/multi-language-pages-access-session-from-a-model/51047>\
**Category:** Rails\
**Created:** [December 24, 2005, 7:47pm UTC](https://www.ruby-forum.com/t/multi-language-pages-access-session-from-a-model/51047 "2005-12-24T19:47:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_Pronk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@Matthias\_Pronk](https://www.ruby-forum.com/u/Matthias_Pronk)\
**Post date:** [December 24, 2005, 7:47pm UTC](https://www.ruby-forum.com/t/multi-language-pages-access-session-from-a-model/51047/1 "2005-12-24T19:47:53Z")

</div>

Hi,

I’m creating a simple Ruby on Rails based CMS. It will support multiple  
languages. Therefore I’ve a table ‘pages’ and a table  
‘page\_translations’. The table ‘page’ contains a title and body in the  
default language, the table ‘page\_translations’ contain a title and body  
for an other language.

Now I would like to have an easy way to retrieve the title and body of a  
page given the current language (stored in the session). I would like to  
put this logic in a model (the Page model, or maybe a new model like  
‘TranslatedPage’). This way I don’t have to put ugly code in my  
controllers and views everytime to fetch the page for the current  
language.

However, it seems I can’t access @session from the model. Does anybody  
have a good idea on how to implement this?

Thanks in advance!

Greetings,

- Matthias

---

<div class="post-metadata">

**Author:** ![Matthias\_Pronk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@Matthias\_Pronk](https://www.ruby-forum.com/u/Matthias_Pronk)\
**Post date:** [December 24, 2005, 8:30pm UTC](https://www.ruby-forum.com/t/multi-language-pages-access-session-from-a-model/51047/2 "2005-12-24T20:30:40Z")

</div>

On 24 Dec 2005, at 18:47, Matthias Pronk wrote:

> However, it seems I can’t access @session from the model. Does anybody  
> have a good idea on how to implement this?

Try dropping the @ – use session instead of @session. Any good?

## Yours, Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed\_email\_address@domain.invalid  
[Xeriom.NET](http://Xeriom.NET) | f: +44 (0)709 287 1902 | w: [http://xeriom.net](http://xeriom.net)

---

<div class="post-metadata">

**Author:** ![Matthias\_Pronk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6a8cbe/32.png) [@Matthias\_Pronk](https://www.ruby-forum.com/u/Matthias_Pronk)\
**Post date:** [December 24, 2005, 10:28pm UTC](https://www.ruby-forum.com/t/multi-language-pages-access-session-from-a-model/51047/3 "2005-12-24T22:28:12Z")

</div>

Matthias Pronk a Ã©crit :

> controllers and views everytime to fetch the page for the current  
> language.
> 
> However, it seems I can’t access @session from the model. Does anybody  
> have a good idea on how to implement this?

My 0.02$: I think you should normalize your tables, and have

DEFAULT\_LANG= ‘en’

page\_translations  
id  
lang CHAR(2) DEFAULT en  
mycontent TEXT

then in your controller you simply do

MyController.show  
@my\_model = MyModel.find(12, :include =\> :page\_translations,  
:conditions =\> [‘lang = ?’, session[:lang]])  
end

Why is this ugly ?
