My class name conflicting with Ruby's
def initialize
today = ::Date.today # get today's date from Ruby's Date class
puts "Today's date is #{today.to_s}"
end
What is double colon in Ruby
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
Conflicting Class Name in Rails
Using include
messes up namespaces, so it's best to just not do that. Ruby has a singular global namespace unlike things like Python and Node, so you need to be very careful when importing.
If you include SendGrid
and that has a SendGrid::Attachment
class then Ruby will try and reconcile that with your Attachment
class later, which will fail.
The best plan is to update your code to include the SendGrid::
prefix on everything. A well-designed library will be cleanly namespaced to avoid this problem.
Resolving a class name conflict in a Rails application
I believe the issue is down to Ruport requiring the PDF::Writer gem, which in turn requires the Transaction::Simple gem which defines the module Transaction.
There is certainly a #transaction method in ActiveRecord, but I do not think there is a Transaction module or class within Rails. I'll be happy to be corrected on that one.
Namespacing is usually the best practice for avoiding naming conflicts like this. E.g.
module Account
class Transaction < ActiveRecord::Base
....
end
end
However, namespacing ActiveRecord models may throw up other issues.
As time consuming as it may be, renaming your Transaction model may be the best bet.
You can still keep your existing transactions database table if you wanted, so your migrations don't need to change, by putting self.table_name = "transactions"
inside your model.
Your associations with other models can also still be named "transaction(s)" by specifying the class_name in your association call. E.g.
class User < ActiveRecord::Base
has_many :transactions, :class_name => "AccountTransaction"
end
Those two suggestions may or may not save you some time.
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
How to Access HTML Elements That Are Rendered in JavaScript Using Xpath
Rails Object Based Permission/Authorization Engine
Regex for Matching All Words Between a Set of Curly Braces
Iterate JSON with Ruby and Get a Key,Value in an Array
How to Access Text Field in an Iframe
Should Repeat a Number of Times
Does 'Upcase!' Not Mutate a Variable in Ruby
Install Gem Gives "Failed to Build Gem Native Extension."
Actionmailer Smtp "Certificate Verify Failed"
Rails: Merit Gem Badge Not Registering or Displaying
Ruby Daemon Process to Keep Objects Alive for Transient Ruby Instances
Has_Many and No Method Error Issue
Sort by Date in Descending Order in Ruby in Rails
Assign to an Array and Replace Emerged Nil Values
Ruby: Difference Between Read_Timeout and Open_Timeout
Ruby: Why Are All Coordinates Getting Updated in an Array of Arrays