« Back to blog

Sharing relations between translations: the easy way

One of the tickets about i18n recently closed was #316. This ticket added a new method to define which associations are shared between the translations of a given instance.

An example of a shared translation behaviour can be viewed here:

This is extracted from last week's post about i18n. Before these changes, the way to define this kind of special association was to use the :translation_shared key:

This is ok in some cases (although maybe the option name is not clear enough), but what happens when you have a lot of associations that behave this way?

Not nice, repetitive and what's worse, even confusing sometimes, if you have these mixed with some others that are not :translation_shared.

The new method fixes this by allowing to define in a single call all these associations:

Pretty better IMHO. Allows you to define in a single line all the associations that have this special behaviour, so you can have a better tracking of what's going on in the model. And you can leave the Rails association declarations as usual.

The old :translation_shared option has not been deprecated, and continues to be a valid option sometimes (e.g. in simple models). But generally I find myself using the new method, as it also allows more extensibility -you don't need to call it at the same place where you are declaring the association, so it can be placed in an external library that extends your model. 

Last but not least, this new method also works seamlessly with media_attachments:

 

Comments (0)

Leave a comment...