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

Fix InverseOfAssociationNotFoundError when class is called Translation #818

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vollnhals
Copy link

We have a classed called Translation that translates a value.

class Translation < ApplicationRecord
  translates :value
end

This causes the following error:

      ActiveRecord::InverseOfAssociationNotFoundError:
        Could not find the inverse association for globalized_model (:translations in Translation)
      # /home/circleci/bundle/ruby/2.7.0/gems/activerecord-5.2.8.1/lib/active_record/reflection.rb:236:in `check_validity_of_inverse!'

The problems seems to be that globalize also uses classes called Translation internally. This results in a confusion between Translation and Translation::Translation when Rails checks the inverse_of.

I fixed this by prepending "::" to the class name, making sure that the class is loaded from the top-level namespace.

We have a classed called `Translation` that translates a value.

```
class Translation < ApplicationRecord
  translates :value
end
```

This causes the following error:
```
      ActiveRecord::InverseOfAssociationNotFoundError:
        Could not find the inverse association for globalized_model (:translations in Translation)
      # /home/circleci/bundle/ruby/2.7.0/gems/activerecord-5.2.8.1/lib/active_record/reflection.rb:236:in `check_validity_of_inverse!'
```

The problems seems to be that globalize also uses classes called Translation internally.

This results in a confusion between `Translation` and `Translation::Translation` when Rails checks the inverse_of.

I fixed this by prepending `"::"` to the class name, making sure that the class is loaded from the top-level namespace.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant