Rails Globalize3 获取所有对象,无论语言环境如何
我有一个文章模型,我想检索所有条目,无论其区域设置如何。
Article.all 仅返回原始对象(存储在 posts 表中的对象),而不返回其翻译(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
I have an Article model and I would like to retrieve all entries regardless of their locale.
Article.all returns only the original objects (those stored in the articles table) without their translations (translatable fields in the article_translations table). Also, objects with a different locale than the current I18n.locale have their fields set to nil (?).
Article::Translation.all does return all objects regardless of language, but only from the translation class (article_translations table - which means only the fields that are set as translatable).
I'm using Rails 3.0.7 and Globalize3 0.1.0 BETA.
This is the model:
class Article < ActiveRecord::Base
translates :title, :content, :slug, :published_at, :created_at, :updated_at
end
This is the migration file:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在两个表中都有重复的 :title、:content、:slug。根据您的模型定义,它们应该只出现在转换表中。 :-)
You have :title, :content, :slug duplicated in both tables. By your model definition, they should only be in the translation table. :-)