# Need some implementation ideas :-)

**URL:** <https://www.ruby-forum.com/t/need-some-implementation-ideas/89379>\
**Category:** Rails\
**Created:** [January 4, 2006, 2:28pm UTC](https://www.ruby-forum.com/t/need-some-implementation-ideas/89379 "2006-01-04T14:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 4, 2006, 2:28pm UTC](https://www.ruby-forum.com/t/need-some-implementation-ideas/89379/1 "2006-01-04T14:28:18Z")

</div>

Hi,

I have good this cool challenge from my boss on developing a very simple  
cms-like system to publish articles within. The caveat is that he wants  
to have multi-lingual content.  
At first I thought RoR would be perfect for the assignment but right now  
I’m not so sure.

My first attempt involved creating a table for each model/languagde like  
this:  
articles\_en  
articles\_dk  
articles\_de  
etc

but then I will dynamically need to change the used table per request.  
Seems like something that will slow down the app quite a bit.

Then I thought I could create the primary key in the table like a joined  
key of the id field and language so I would have these ids:  
1-en  
1-de  
2-en  
2-dk  
etc

But ActiveRecord doesn’t support multiple columns as primary key.

So right now I could some new idea or thought cause I’m sure RoR can do  
everything - it’s only a matter on finding out how to do it 🙂

Greetings,  
Gitte W.

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 4, 2006, 2:59pm UTC](https://www.ruby-forum.com/t/need-some-implementation-ideas/89379/2 "2006-01-04T14:59:32Z")

</div>

How about something like this…

class Article \< ActiveRecord::Base  
has\_many :translations  
end

class Translation \< ActiveRecord::Base  
belongs\_to :article  
validates\_presence\_of :language  
validates\_presence\_of :body  
validates\_presence\_of :title  
end

tables…

articles  
id INT(11) auto\_increment, not null  
other non-language specific fields, like publish time, etc…

translations  
id INT(11) auto\_increment, not null  
article\_id  
language varchar(2)  
title varchar(80)  
body mediumtext

Then you could do something like

Articles.find\_all\_by\_language(‘en’)  
or  
@article = Articles.find\_by\_id\_and\_language(params[:id],‘en’)

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 4, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/need-some-implementation-ideas/89379/3 "2006-01-04T15:00:59Z")

</div>

if necessary you could create a languages table and use a foreign key  
instead of storing the two letter language code.

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 5, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/need-some-implementation-ideas/89379/4 "2006-01-05T17:44:30Z")

</div>

Gitte:  
On Jan 4, 2006, at 4:57 AM, Gitte W. wrote:

> So right now I could some new idea or thought cause I’m sure RoR  
> can do everything - it’s only a matter on finding out how to do it 🙂

Have a table for languages as follows:

id  
articleId  
LanguageCode

in your articles model have :belongs\_to language  
and in your languages model have :has\_many articles  
Cheers,  
Hasan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mamatux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mamatux](https://www.ruby-forum.com/u/mamatux)\
**Post date:** [January 6, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/need-some-implementation-ideas/89379/5 "2006-01-06T18:00:28Z")

</div>

Kevin O. wrote:

> validates\_presence\_of :body  
> id INT(11) auto\_increment, not null  
> article\_id  
> language varchar(2)  
> title varchar(80)  
> body mediumtext
> 
> Then you could do something like
> 
> Articles.find\_all\_by\_language(‘en’)  
> or  
> @article = Articles.find\_by\_id\_and\_language(params[:id],‘en’)

Hi Kevin,

Just wanted to say thanks for the idea - it works like a charm!

Greetings,  
Gitte W.
