残念ながら、Globalize3 を使用してこれを行う方法が見つかりませんでした。理論的には、画像用に別のモデルを追加し、変換された列のリストに image_id を追加することもできましたが (MainModel -> Translation -> Image のようなものにする)、Globalize には文字列以外の列に関する移行の問題があるようです。
Globalize3 を使用する代わりに、locale 属性を持つ別の Image モデルと、ネストされた属性を受け入れるメイン モデルを使用してこれを行いました。次のようなもの:
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
トリッキーな部分は、has_many 関係のすべての画像ではなく、1 つの画像に対してのみネストされた属性を受け入れるフォームを取得することでした。
=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