ruby-on-railsrubymobility

How do I filter records by locale using gem mobility?


I have a table mobility_string_translations, but I do not understand how to get to it.

Now I have three records. Two records in German and one in Spanish. I want to get only those records that are in German.

this not working:

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

Solution

  • First of all you may have a column.(Lets say title.)

    On your model post.rb you will have something like this:

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

    The type is important in order to get the :de records(or the :en).

    At schema.rb wou will see a table mobility_string_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
    

    Well now at your console find your records with locale: :de

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

    As you can see at your schema.rb --> "mobility_string_translations" you can play around with your columns in order to find exactly what you want.