0

I now have the following in my seeds.rb

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

However this creates doubles. So I tried to do a create_or_update instead of create but that doesn't seem to be supported. I am using globalize3

rept
  • 2,086
  • 1
  • 26
  • 44

1 Answers1

2

Based on your comment you could do the following:

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")

I'm not sure if there's a nicer way of writing this, but you could create your own method:

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")
mind.blank
  • 4,820
  • 3
  • 22
  • 49
  • This works, but the problem is that if I change the status to for example: os.translations.where(:status => "Creation", :locale => "en").first_or_create I will have 2 english translations for that OrderStatus – rept May 19 '13 at 11:17
  • Would you always change both fields or would one remain the same? – mind.blank May 19 '13 at 11:26
  • For every OrderStatus there can exist only 1 translation in 1 language, so 1 for en 1 for nl ... So it should search for locale and insert or update text. So one will remain the same (the locale). – rept May 19 '13 at 11:28
  • Great, this works, I just use: os.translations.where(:locale => "nl").first_or_create.update_attributes(:status => "In aanmaak") – rept May 19 '13 at 11:45