Globalize: the select option strop
The second Globalize post in as many days. Since I started I may as well through this one out there too, another one for the great Globalized minds of the interweb. I'm regularly bitten by this problem and didn't uncover a satisfactory workaround yet.
Globalize inelegantly flops when you ask it to load the collection attribute of a model instance if the instances in the collection have translated fields.
To illustrate the difficulty I'm having here's a simple Label <- Release -> Artist type data model that you might find on a multilingual music directory site.
class Label < ActiveRecord::Base has_many :releases has_many :artists, :through=>:releases end
class Release < ActiveRecord::Base belongs_to :label belongs_to :artist end
class Artist < ActiveRecord::Base has_many :releases has_many :labels, :through=>:releases translates :bio # let Globalize handle translation of the artists bio's end
>> Label.create(:name=>"Huge Ape Recordings") >> artist=Artist.create(:name=>"Zombie Gunship", :bio=>"Blah blah, the rest is history.") >> Release.create(:label_id=>1,:artist_id=>1) >> label=Label.find(1) => #"Huge Ape Recordings", "id"=>"1"}> >> label.artists StandardError: :select option not allowed on translatable models (artists.*) from ./script/../config/../config/../vendor/plugins/globalize_extension/lib/globalize/db_translate_ex.rb:31:in `find_every'
I have a feeling (hope) that this kind of thing ought to be possible with Globalize. In either case Google is eerily empty of relevant search results. Can anyone throw any light on this, how are you getting round it?
Localize Models to the rescue
Update: I've just demoed a delightful plugin by Saimon Moore called Localize Models. It's a system that can be used to replace Globalize's model translations. Rather than storing model translations in the main translations table, it uses additional columns with language code suffixes in the model tables. The big relief with this alternative is that there are no restrictions on :select options. Check his page for instructions (NB. The plugin threw errors in rails 1.1.6 but seems to work fine in edge rails).
>> l=Label.find(1)
>> a=l.artists.first
=> # Artist:0x33d4bc4 @attributes={"name"=>"Zombie Gunship",
"bio_nl"=>nil, "id"=>"1", "bio"=>"English bio here."}>
>> a.bio
=> "English bio here."
>> Locale.set(LOCALES['nl'])
>> a.bio="Dutch bio here"
>> a.save
=> true
>> Locale.set(LOCALES['en'])
>> a.bio
=> "English bio here."
>> Locale.set(LOCALES['nl'])
>> a.bio
=> "Dutch bio here"
Lovely!
Trackbacks
Use this link to trackback from your own site.
