Calling obj.i18.pluck in a model with a jsonb backend gives a TypeError

136 views Asked by At

I'm using Mobility 0.8.4 and when I try to pluck an attribute from a model, it raises a TypeError.

# models/skill.rb
class Skill < ApplicationRecord
  extend Mobility

  translates :name, backend: :jsonb
end

# db/schema.rb
create_table "skills" do |t|
  ...
  t.jsonb "name", default: {}
  ...
end

Then...

irb(main):001:0> Skill.pluck(:name).take(5)
   (1.9ms)  SELECT "skills"."name" FROM "skills"
=> [{"pt-BR"=>"Ruby"}, {"pt-BR"=>"Java"}, {"pt-BR"=>"PHP"}, {"pt-BR"=>"Python"}, {"pt-BR"=>"C++"}]

irb(main):002:0> Skill.i18.pluck(:name).take(5)
Traceback (most recent call last):
        1: from (irb):2
TypeError (no implicit conversion of Mobility::Arel::Nodes::Jsonb into String)

Am I doing something wrong?

Thanks

1

There are 1 answers

1
anothermh On

i18 wants to translate strings but you're feeding it JSONB. Try:

Skill.i18.pluck("name -> 'pt-BR'").take(5)

That should pluck the value of the key pt-BR (e.g., Ruby, Java, etc.) and feed it through the i18 translator, rather than supplying the full JSONB payload to the translator.