How to make localized Paperclip attachments with Globalize3?

globalize3, paperclip, ruby, ruby-on-rails, ruby-on-rails-3

Solution

Unfortunately I didn't find a way to do this using Globalize3. In theory, I could have added a separate model for image and add image_id to list of translated columns (to have something like MainModel -> Translation -> Image), but it seems that Globalize has some migration issues with non-string columns.

Instead of using Globalize3, I did this with a separate Image model with locale attribute and main model which accepts nested attributes for it. Something along the lines of:

class MainModel < ActiveRecord::Base
  has_many :main_model_images
  accepts_nested_attributes_for :main_model_images

  # return image for locale or any other as a fallback
  def localized_image(locale)
    promo_box_images.where(:locale => locale).first || promo_box_images.first
  end
end

class MainModelImage < ActiveRecord::Base
  belongs_to :main_model
  has_attached_file :image

  validates :locale,
    :presence => true,
    :uniqueness => { :scope => :main_model_id }
end

Tricky part was getting form to accept nested attributes only for one image, instead of all images in has_many relation.

=f.fields_for :main_model_images, @main_model.image_for_locale(I18n.locale) do |f_image|
  =f_image.hidden_field :locale
  =f_image.label :image

Problem

I have a project using Paperclip gem for attachments and Globalize3 for attribute translation. Records need to have a different attachment for each locale. I though about moving Paperclip attributes to translation table, and that might work, but I don't think that would work when Paperclip needs to delete attachments. What's the best way to achieve something like that? UPDATE: to be clear, I want this because my client wants to upload different images for each locale.

Original source