如何使用 Gem 移动性按区域设置筛选记录?



我有一个表格mobility_string_translations,但我不明白如何到达它。

现在我有三条记录。两条德语记录和一条西班牙语记录。我只想得到那些德语记录。

这不起作用:

all_de_posts = Post.i18n.find_by(locale: :de)

首先,你可能有一个列。比方说标题。

在您的模型post.rb上,您将拥有如下所示的内容:

class Post < ApplicationRecord
extend Mobility
translates :title,  type: :string, locale_accessors: [:en, :de]
end     

为了获取 :d e 记录(或 :en(,该类型很重要。

schema.rbwou 会看到一个mobility_字符串_translations的表格

create_table "mobility_string_translations", force: :cascade do |t|
t.string "locale", null: false
t.string "key", null: false
t.string "value"
t.integer "translatable_id", null: false
t.string "translatable_type", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["translatable_id", "translatable_type", "key"], name: "index_mobility_string_translations_on_translatable_attribute"
t.index ["translatable_id", "translatable_type", "locale", "key"], name: "index_mobility_string_translations_on_keys", unique: true
t.index ["translatable_type", "key", "value", "locale"], name: "index_mobility_string_translations_on_query_keys"
end

现在在您的控制台上找到带有区域设置的记录::d e

irb:> Post.all
irb:> Mobility::ActiveRecord::StringTranslation.where(locale: :de)

正如你在schema.rb中看到的 -->"mobility_string_translations",你可以玩弄你的列,以便准确地找到你想要的东西。

最新更新