0

globalize2를 사용하여 이전 사이트에 i18n을 추가했습니다. 이미 스페인어로 많은 내용이 있지만 globalize2 테이블에는 저장되지 않습니다. 레일즈로 마이그레이션하면서이 컨텐츠를 globalize2로 변환 할 수 있습니까?Globalize2 및 마이그레이션

>> Panel.first 
=> #<Panel id: 1, name: "RT", description: "asd", proje.... 
>> Panel.first.name 
=> nil 
>> I18n.locale = nil 
=> nil 
>> Panel.first.name 
=> nil 

어떤 아이디어 :

문제는 내가 저장된 콘텐츠에 액세스 할 수있다?

답변

1

나는이 방법을 다른 방법으로 해결했다고 확신하지만 여기에 있습니다. read_attribute 메서드를 사용하여 원하는 것을 파고들 수 있습니다.

다음을 사용하여 주 테이블의 컨텐츠를 globalize2 변환 테이블로 마이그레이션했습니다.

  1. 해당 모델에 translates 행을 추가하십시오.
  2. 놓습니다 config/initializers/globalize2_data_migration.rb에서 다음

    require 'globalize' 
    module Globalize 
        module ActiveRecord 
        module Migration 
    
         def move_data_to_translation_table 
         klass = self.class_name.constantize 
         return unless klass.count > 0 
         translated_attribute_columns = klass.first.translated_attributes.keys 
         klass.all.each do |p| 
          attribs = {} 
          translated_attribute_columns.each { |c| attribs[c] = p.read_attribute(c) } 
          p.update_attributes(attribs) 
         end 
         end 
    
         def move_data_to_model_table 
         # Find all of the translated attributes for all records in the model. 
         klass = self.class_name.constantize 
         return unless klass.count > 0 
         all_translated_attributes = klass.all.collect{|m| m.attributes} 
         all_translated_attributes.each do |translated_record| 
          # Create a hash containing the translated column names and their values. 
          translated_attribute_names.inject(fields_to_update={}) do |f, name| 
          f.update({name.to_sym => translated_record[name.to_s]}) 
          end 
    
          # Now, update the actual model's record with the hash. 
          klass.update_all(fields_to_update, {:id => translated_record['id']}) 
         end 
         end 
        end 
        end 
    end 
    
  3. 은 다음과 마이그레이션을 만든 :

    class TranslateAndMigratePages < ActiveRecord::Migration 
        def self.up 
        Page.create_translation_table!({ 
         :title => :string, 
         :custom_title => :string, 
         :meta_keywords => :string, 
         :meta_description => :text, 
         :browser_title => :string 
        }) 
    
        say_with_time('Migrating Page data to translation tables') do 
         Page.move_data_to_translation_table 
        end 
        end 
    
        def self.down 
        say_with_time('Moving Page translated values into main table') do 
         Page.move_data_to_model_table 
        end 
        Page.drop_translation_table! 
        end 
    end 
    

글로벌화 3 refinerycms에서 많이 빌린다.

+0

와우, 이것에 대해 잊어 버렸고, 우리는 얼마 전에 globalize 사용을 중단했습니다! – diegogs