Forum: Ruby on Rails Need some implementation ideas :-)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8aa68df8d14eff5681c8e87a073e8cca?d=identicon&s=25 Gitte Wange (Guest)
on 2006-01-04 14:28
(Received via mailing list)
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 Wange
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 14:59
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')
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 15:00
if necessary you could create a languages table and use a foreign key
instead of storing the two letter language code.
895a112c66d93cefcd06d2cf371219e6?d=identicon&s=25 Hasan Diwan (Guest)
on 2006-01-05 17:44
(Received via mailing list)
Gitte:
On Jan 4, 2006, at 4:57 AM, Gitte Wange 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 Diwan <hasan.diwan@gmail.com>
8aa68df8d14eff5681c8e87a073e8cca?d=identicon&s=25 Gitte Wange (Guest)
on 2006-01-06 18:00
(Received via mailing list)
Kevin Olbrich 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 Wange
This topic is locked and can not be replied to.