Skip to content

Instantly share code, notes, and snippets.

@brenes
Created August 27, 2012 12:02
Show Gist options
  • Save brenes/3487849 to your computer and use it in GitHub Desktop.
Save brenes/3487849 to your computer and use it in GitHub Desktop.
How to solve Globalize3 issue with old migrations when adding translated fields
There's an issue on globalize when adding or removing fields from the translations that causes the original migrations don't work properly (https://github.com/svenfuchs/globalize3/issues/131).
In my example I have a post model whih I create with some translated fields: name, slug and body. Some days later, I realise I need an excerpt field with a summarized version of the post and it has to be translated, so I create a migration to add this field.
Unfortunately, when you create the translation table it takes not only the fields you set in the params (:name => :string, :slug => :string, :body => :text) but also the params specified to the translates method in the model (:excerpt included) and creates a column for each one of those.
Solution here is to overwrite the translated attribute names of the class in the migration, so when you execute a migration you know which translated attributes will be declared and not need to worry about attributes that are not declared when you create the migration.
Overwriting these attributes is as simple as Post.translated_attribute_names = [:name, :slug, :body]
It's only necessary for the original creation migration, as there's no easy way to add translated fields to the translation table (https://github.com/svenfuchs/globalize3/issues/98), but I think it could be useful to explictly point out what translated fields exist at the moment you add a new column to the translations table.
It could be quite simple to fork the project and create a patch based on this simple idea, but while I find the time to do it I think this may help some people out there.
* Migrations are modifications of a real project and may not be fully functional as they have not really been tested. What I have tested is the idea of overwriting the translated_attributes and it works fine.
class Post
translates :name, :slug, :body, :excerpt
end
class AddExcerptToPost < ActiveRecord::Migration
def up
Post.translated_attribute_names = [:name, :slug, :body, :excerpt]
add_column :post_translations, :excerpt, :string
end
def down
remove_column :post_translations, :excerpt
end
end
class AddExcerptToPost < ActiveRecord::Migration
def up
add_column :post_translations, :excerpt, :string
end
def down
remove_column :post_translations, :excerpt
end
end
class CreatePosts < ActiveRecord::Migration
def up
create_table :posts do |t|
t.string :author
t.timestamps
end
Post.translated_attribute_names = [:name, :slug, :body]
Post.create_translation_table! :name => :string, :slug => :string, :body => :text
end
def down
drop_table :posts
Post.drop_translation_table!
end
end
class CreatePosts < ActiveRecord::Migration
def up
create_table :posts do |t|
t.string :author
t.timestamps
end
Post.create_translation_table! :name => :string, :slug => :string, :body => :text
end
def down
drop_table :posts
Post.drop_translation_table!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment