Rails Model Name Conflict with Included Gem

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.

Ruby 2.4 module name conflict with my model class name

Warning module is used for overriding ruby warn method. To temporarily get around the error - you can undefine the constant before defining your model:

Object.send(:remove_const, :Warning)

runnable test:

require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "5.2.3"
gem "sqlite3", "~> 1.3.6"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table(:warnings, force: true){|t| t.string :name }
end

RubyWarning = Warning
Object.send(:remove_const, :Warning)

class Warning < ActiveRecord::Base
end

def RubyWarning.warn(str)
puts "still works: #{str}"
end

class SomeTest < Minitest::Test
def test_stuff
wrn = Warning.create name: 'test'
assert_equal(wrn.name, 'test')
warn "Test warn"
end
end

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.

Rails/ActiveRecord .where method naming collision with Ruby Gem class method

Classes are namespaces.

If you define where on your own class and Rails defines where on another class you will not have a conflict. This is the case for both instance methods and class methods, which after all are just instance methods on the singleton class of the class.

However, if you were to define where on Object or another top-level class then you'll run into plenty of conflict because all classes are subclassing these classes.

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.

Ruby Rails: How Rails handle same method name in in two different gems

To answer your question directly: This is ultimately the responsibility of the developer who uses the gems. The authors of gems should take some care to name their methods appropriately, but nothing they can (or should) do will obviate the possibility of a collision.

This feature of Ruby is very powerful, but as always, with great power comes great potential for disaster.

Ruby Module name conflict

Why doesn't it work? Nothing says you can't insert your own classes into the Foursquare module space. Not that it's necessarily a good idea, but the language allows it no problem.

conflicting ruby gems

There's probably no elegant solution to the problem. If you really need the two gems working side by side I think your best option is to fork one of them (or possibly both) and use your fork. This is how I'd go about it:

  • If either gem is hosted on Github then fork it, or if both are on Github fork the one that seems to be the least work.
  • If neither gem is on Github, see if you can get hold of the source (grabbing it from the gem is a possibility, but finding the real repository might be helpful as there may be other files there that are not included in the gem), and put it in a repository on Github. Make sure the gem's license allows this (which it almost certainly does if it's one of the common open source licenses).
  • Make your changes.
  • Make sure there is a .gemspec file in the root of the repository, the next step will not work otherwise.
  • Use Bundler to manage your projects dependencies. Instead of specifying the dependency to the library you've modified as

    gem 'the_gem'

    specify it like this:

    gem 'the_gem', :git => 'git://github.com/you/the_gem.git'

    (but change the URL to the repository to the actual one)

  • Send an e-mail to the maintainer of the gem you modified and ask him or her to consider merging in your changes in the next release.

Bundler makes it really easy to use alternative versions of gems with minimal hassle. I often fork gems, fix bugs or add features, change my Gemfile to point to my version, then ask the maintainer to merge in my changes. When, or if, that happens I simply change my Gemfile back to just refer to the official version of the gem.

An alternative strategy, if the maintainer does not want to merge in your changes and you want to distribute your version to others is to push up your version to Rubygems as a new gem, but in that case prefix the gem name with your name, or some other string that identifies your gem as a variant.



Related Topics



Leave a reply



Submit