ruby on rails - Globalize3 and creating object in all languages -
i have model named menu has product_types belong it.
upon creation of menu want set default product_types:
i have constant:
defaults = [ { name_en: 'white bread', name_nl: 'wit brood', name_fr: 'pain blanc'}, { name_en: 'brown bread', name_nl: 'bruin brood', name_fr: 'pain brun' } ]
and should create translations error @ moment saying:
"you cannot call create unless parent saved"
def create_defaults producttype::defaults.each |pt| t = product_types.create t.translations.create(locale: 'en', name: pt[:name_en]) t.translations.create(locale: 'nl', name: pt[:name_nl]) t.translations.create(locale: 'fr', name: pt[:name_fr]) end
the menu can in 3 languages (selectable user) , user has default language. default language can dutch, english or french. it's possible have dutch user languages of menu in dutch , french.
the code tried before (that didn't give error msg) was:
def create_defaults producttype::defaults.each |pt| t = product_types.create(name: pt[:name_en]) t.translations.create(locale: 'nl', name: pt[:name_nl]) t.translations.create(locale: 'fr', name: pt[:name_fr]) end end
the problem procedure is: suppose user default language set dutch , language of menu set dutch (no other languages). default created types appear in english...
this trick:
def create_defaults producttype::defaults.each |pt| t = product_types.create t.translations.build(locale: 'en', name: pt[:name_en]) t.translations.build(locale: 'nl', name: pt[:name_nl]) t.translations.build(locale: 'fr', name: pt[:name_fr]) t.save end end
when saving parent child objects saved well.
Comments
Post a Comment