Forum: Ruby on Rails constant redefinition warning

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kad K. (Guest)
on 2009-01-31 20:44
(Received via mailing list)
I get a redefinition warning :

log warning =>
/Users/myself/Developpement/MyApp/vendor/plugins/globalize2/lib/myapp/
validations.rb:18: warning: already initialized constant
RoleTranslation

I am using the following code, apps is running well w warning
module Validations
    def validate_translation_uniqueness_of(*attr_names)
translation_class = Object.const_set "#{self.class.name}Translation",
Class.new(::ActiveRecord::Base){}
...

I tried now to avoid it writing :
translation_class = Object.const_set "#{self.class.name}Translation",
Class.new(::ActiveRecord::Base){} unless defined?("#{self.class.name}
Translation")
but it fails ...  (obviously don't like a string..)

I also tried:
translation_class = Object.const_set "#{self.class.name}Translation",
Class.new(::ActiveRecord::Base){} unless defined?("#{self.class.name}
Translation".to_sym)

"#{self.class.name}Translation".to_sym => :AccountingPeriodTranslation
which is also bad..

I need to have : #{self.class.name}Translation  giving a class , how
should I write it ?

 defined?(AccountingPeriodTranslation) =>"constant"  is what I should
test  (or any other  xxxxTranslation class where xxxx is   self.class

thanks for your help

erwin
Kad K. (Guest)
on 2009-02-01 00:29
(Received via mailing list)
I changed the creation + test to a simple get constant as it's already
declared (being an ActiveRecord class created by Globalize plugin)

a simple :

 globalize_class = Object.const_get "#{self.class.name}Translation"

gives me the class...
This topic is locked and can not be replied to.