Globalize 2: is it possible to search on translated fields ?

304 views
Skip to first unread message

giates

unread,
Feb 11, 2009, 4:53:35 PM2/11/09
to rails-i18n
Hi to all the crew,

I'm starting a new project and I want to use Globalize 2 for model
translations.

I cannot find a good solution for searching on translated columns: ex.

script/generate model Article published:boolean

Article model: translates :title, :text

and then: Article.create_translation_table! :title => :string, :text
=> :text

How can I do a search like this below ?

Article.all(:conditions => ["published and title like ?", params
[:title]])

Seems that I cannot access the translations table ex.:

Article.all(:conditions => ["articles.published and
article_translations.title like ?", params[:title]])

Seems that I cannot include translations asociations ex.:

Article.all(:iclude => article_translations, :conditions =>
["articles.published and article_translations.title like ?", params
[:title]])

So I try to create a model ArticleTranslation and searching using
this:

Article.scoped(:conditions => {:published => nil}).find_by_title
("test")

Not too much elegant but it works.

But wait a moment: what if I want to search using a "like" sql
conditions ?

I cannot find a solution .someone can help me ?

Many thanks in advance...

Mirko

unread,
Feb 12, 2009, 7:33:11 AM2/12/09
to rails-i18n
Try using :join => :globalize_translations instead of :include =>
article_transtlations

This is pasted from my app:

@proizvodjaci = Proizvodjac.find(:all, :joins
=> :globalize_translations, :conditions => "locale = 'sr'", :order =>
"naziv ASC")

# proizvodjaci = manufacturers, naziv = title

Greets,
Mirko

Gianluca Tessarolo

unread,
Feb 12, 2009, 11:10:48 AM2/12/09
to rails...@googlegroups.com
Wow !

Many thanks Mirko, it works very well !

Just a little question: are you using Globalize 2 in production ?

Mirko

unread,
Feb 12, 2009, 7:26:40 PM2/12/09
to rails-i18n
Yes I am, but i don't know how smart that is, since I'm not familiar
with Globalize2 future plans but it sure feels much better than
Globalize1 I was using.

Cheers,
Mirko

On Feb 12, 5:10 pm, Gianluca Tessarolo

Gianluca Tessarolo

unread,
Feb 13, 2009, 2:53:39 AM2/13/09
to rails...@googlegroups.com
Ok Mirko, I'm happy to see that you are using globalize2 in production...

I was using Globalize1 too but I want to use a better db schema approach like :globalize_translations uses.

I encountered some problems using Globalize2:

- I cannot search on translated fields (solved by you, many thanks again)
- I cannot validate uniqueness of translated fields (solved by http://codesnippets.joyent.com/user/kadoudal#post1960)

- Translated records are not destroyed when I destroy parent record: should I set referential integrity on database or is there any solution to this issue ?

Many thanks...
Reply all
Reply to author
Forward
0 new messages