partydrone
partydrone

Reputation: 537

Deleting a single translation from a model in a Rails project using Mobility

I'm migrating a Rails project from Globalize to Mobility. With Globalize, I had access to the current translation through the translation method:

feature.translation

I used this mainly when managing translations for a model to delete a specific translation:

feature.translation.destroy

With Globalize, for each object, I get the list of translations and create a delete button for each:

<%= link_to 'x', admin_feature_path(list_item, translation_locale: l), method: :delete, data: { confirm: %(Are you sure you want to delete this #{humanize_locale l} translation? This cannot be undone.) } %>

I use the locale passed in with the link to delete the appropriate translation:

Mobility.with_locale(translation_locale) do
  @feature.tranlsation.destroy
end

Is there a straight-forward way of deleting a single translation from a model with multiple translations?

Upvotes: 0

Views: 327

Answers (1)

Chris Salzberg
Chris Salzberg

Reputation: 27374

There's really nothing special about Mobility here. You've got a model with an association translations, each translation has a locale.

So to destroy a translation, you can just find it and destroy it:

translation = feature.translations.find { |t| t.locale == Mobility.locale.to_s }
translation.destroy

If you want the method like Globalize has, just add it to your model:

def translation
  translations.find { |t| t.locale == Mobility.locale.to_s }
end

That's it!

Upvotes: 1

Related Questions