How to Resolve Rails Model Namespace Collision

How to resolve Rails model namespace collision

Two solutions:

1) Change your app's Term model to be something else.

2) Patch term-ansicolor to have a namespaced Term and use that gem instead.

Is there a graceful way to refactor a namespace collision with Ruby/Rails gems?

Unfortunately there's no way to influence which modules (namespaces) a gem will use. Files that are required by Ruby will always be evaluated in the global scope.

It's best practice for gem authors to use a module with the same name of the gem, so usually you know what to expect when you install a gem.

Apart from not using Turn, the only solutions are to change your model's name or place it inside a namespace.

Resolving a name collision in 3rd party gem code


  • If it's in your own code (not applicable in this particular case): namespace your friggin constants!
  • If it's in someone else's code: file a bug telling them to namespace their friggin constants! … and wait for them to release a new version …

There's really not more you can do.

How to resolve a rails model attribute and gem name conflict?

You can use read_attribute:

@car.read_attribute(:rate)

but I suggest that you resolve the conflict by renaming one of them as it will just cause you problems and confusion down the road.

You don't need to rollback your database to rename a column, you just have to create a new migration that does so.

How to detect name collision error with Active Record in development mode

All you have to do to test this enum is reference the class so that it is evaluated. When you have no automated tests things blow up in production. That's as constant as death and taxes.

require 'test_helper'

class ProductTest < ActiveSupport::TestCase
test "it does not blow up"
Product
assert true
end
end

The reason an error is raised on start-up in production and not in development is the config.eager_load setting which loads all your models on startup. And no - you don't want to set it to true in in development to catch this error as it will mess up code reloading.

Rails Model name conflict with included gem

include SendGrid it the culprit.

It adds all constants from the SendGrid module to the current module (which is probably the top level), so you can use SendGrid's classes without prefix, e.g. just Email.new instead of SendGrid::Email.new.

The downside is that it also messes with your existing constants.

You can either include it under a specific module:

class Notifications::WelcomeWorker
include Sidekiq::Worker
include SendGrid

# ...
end

Then, Email resolves to Sendgrid::Email and ::Email resolves to your top-level Email class.

Or you could simply remove the include SendGrid line and use the SendGrid:: prefix.



Related Topics



Leave a reply



Submit