2013-05-19 49 views
0

我現在在我的seeds.rb以下如何在seeds.rb中爲create_or_update進行翻譯?

os = OrderStatus.find(1) 
os.translations.create(:status => "In aanmaak", :locale => "nl") 
os.translations.create(:status => "Creating", :locale => "en") 

然而,這造成雙打。所以我試圖做一個create_or_update而不是create,但似乎沒有被支持。我使用globalize3

回答

2

基於您的評論,你可以做到以下幾點:

os = OrderStatus.find(1) 
os.translations.where(locale: "nl").first_or_create.update_attributes(status: "In aanmaak") 
os.translations.where(locale: "en").first_or_create.update_attributes(status: "Creating") 

我不知道是否有寫這個的一個更好的方式,但你可以創建自己的方法:

class Translation < ActiveRecord::Base 
    def first_or_update(locale, status) 
    where(locale: locale).first_or_create.update_attributes(status: status) 
    end 
end 

os.translations.first_or_update("en", "Creating") 
+0

這可行,但問題是,如果我將狀態更改爲例如: os.translations.where(:status =>「Creation」,:locale =>「en」)。first_or_create 我將有2該訂單狀態的英文翻譯 – rept

+0

你會一直這兩個領域的變化還是會保持不變? –

+0

對於每個OrderStatus,在1種語言中只能存在1個翻譯,因此1對於nl只有1個...因此它應該搜索區域設置並插入或更新文本。所以一個會保持不變(區域設置)。 – rept