我有一个 Article 模型,我想检索所有条目,无论它们的语言环境如何。
Article.all 仅返回原始对象(那些存储在文章表中的对象)而不返回它们的翻译(article_translations 表中的可翻译字段)。此外,与当前 I18n.locale 具有不同语言环境的对象的字段设置为 nil (?)。
Article::Translation.all 确实返回所有对象,无论语言如何,但仅来自翻译类(article_translations 表 - 这意味着仅设置为可翻译的字段)。
我正在使用 Rails 3.0.7 和 Globalize3 0.1.0 BETA。
这是模型:
class Article < ActiveRecord::Base
translates :title, :content, :slug, :published_at, :created_at, :updated_at
end
这是迁移文件:
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :title
t.text :content
t.string :slug
t.boolean :published
t.datetime :published_at
t.timestamps
end
add_index :articles, :slug
Article.create_translation_table! :title => :string,
:content => :text,
:slug => :string,
:published_at => :datetime,
:created_at => :datetime,
:updated_at => :datetime
end
def self.down
Article.drop_translation_table!
drop_table :articles
end
end