我正在使用Mobility 0.8.4,当我尝试从模型中选择属性时,会引发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
然后...
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)
我做错什么了吗?
谢谢
答案 0 :(得分:0)
i18
要翻译字符串,但您要向其提供JSONB。试试:
Skill.i18.pluck("name -> 'pt-BR'").take(5)
这应该选择键pt-BR
(例如Ruby
,Java
等)的值,并将其通过i18转换器提供,而不是将完整的JSONB有效负载提供给译者。