Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a TypeError when calling obj.i18n.pluck #308

Closed
mendelgusmao opened this issue Nov 28, 2018 · 12 comments
Closed

Getting a TypeError when calling obj.i18n.pluck #308

mendelgusmao opened this issue Nov 28, 2018 · 12 comments

Comments

@mendelgusmao
Copy link

When I try to pluck an attribute from a model that uses the jsonb backend, it raises a TypeError (no implicit conversion of Mobility::Arel::Nodes::Jsonb into String).

Context

I'm using Mobility 0.8.4, Rails 5.1.6 (ActiveRecord 5.1.6), PostgreSQL 10. Here is an excerpt of my code:

# initializers/mobility.rb
Mobility.configure do |config|
  config.default_backend = :jsonb
  config.accessor_method = :translates
  config.query_method = :i18n
  config.default_options[:default] = proc { |attribute| self[attribute.to_sym] }
  config.default_options[:dirty] = true
end

# models/skill.rb
class Skill < ApplicationRecord
  extend Mobility
  ...
  translates :name 
end

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

Actual Behavior

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.i18n.pluck(:name).take(5)
Traceback (most recent call last):
        1: from (irb):2
TypeError (no implicit conversion of Mobility::Arel::Nodes::Jsonb into String)

Thanks for the support!

@shioyama
Copy link
Owner

This looks a bit fishy to me:

config.default_options[:default] = proc { |attribute| self[attribute.to_sym] }

what if you take that out?

@shioyama shioyama self-assigned this Nov 28, 2018
@shioyama
Copy link
Owner

Also, please post Gemfile or mention what other gems might be involved.

@mendelgusmao
Copy link
Author

This looks a bit fishy to me:

config.default_options[:default] = proc { |attribute| self[attribute.to_sym] }

what if you take that out?

Removing the :default option didn't change anything but your clue made me remove the :dirty option and now it works. Thank you!

@shioyama
Copy link
Owner

Hmm... ok so maybe there's a compatibility issue with Dirty then.

@shioyama shioyama reopened this Nov 29, 2018
@shioyama
Copy link
Owner

Re-opened this since I'd like to figure out where the compatibility issue is.

@mendelgusmao
Copy link
Author

And maybe an incompatibility with :fallbacks and :default. Using the same setup, using fallbacks = :en => :'pt-BR' or the aforementioned default lambda:

irb(main):005:0> Mobility.with_locale(:en) { Industry.i18.pluck(:name).take(5) }
   (0.8ms)  SELECT ("industries"."name" ->> 'en') FROM "industries"
=> [nil, nil, nil, nil, nil]

Which is pretty comprehensible because only the :en translation is loaded. But what I found weird is that the lambda is not even being called. Should I open another issue?

@shioyama
Copy link
Owner

Should I open another issue?

No, this is not an issue, it is simply not supported. When you call pluck, you are doing a query, and things like dirty tracking and fallbacks are not supported on query methods. Although fallbacks on querying may be supported in the future, see: #51.

However, the problem you originally found in this issue would be a bug, once I can confirm it's not related to anything outside of Mobility.

@shioyama
Copy link
Owner

Ok I've reproduced this, seems to only happen in Rails 5.1.x and not 5.2.x.

@shioyama
Copy link
Owner

@mendelgusmao #309 should fix this issue with dirty enabled. I'll merge and release the fix once specs pass.

@shioyama
Copy link
Owner

This is fixed on master and on the 0-8-stable branch. Please try the latter (gem 'mobility', github: 'shioyama/mobility', branch: '0-8-stable') and see if it works.

@shioyama
Copy link
Owner

Fix released in 0.8.7.

@mendelgusmao
Copy link
Author

Thank you, @shioyama

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants