Reputation: 590
I need to extend a model in a Rails 2.3.11 app without touching the original source file. I need to add a :has_many association
in it. I've tried the approach mentioned in Extend model in plugin with "has_many" using a module without success. The class I need to extend is called UbiquoUser
. Here the code I have in lib/extensions.rb
:
module Sindicada
module Extensions
autoload :UbiquoUser, 'extensions/ubiquo_user'
end
end
UbiquoUser.send(:extend, Sindicada::Extensions::UbiquoUser)
Here's what I have in lib/extensions/ubiquo_user.rb
:
module Sindicada
module Extensions
module UbiquoUser
module ClassMethods
def has_audio_favorites
has_many :audios, :through => :audios_favorite
end
end #ClassMethods
def self.included(base)
base.extend(ClassMethods).has_audio_favorites
end
end #UbiquoUser
end #Extensions
end #Sindicada
However, when I try to access the property audios of UbiquoUser
on the app I get the error undefined method audios for class blablabla
.
I also have the require 'extensions'
in the environment.rb
file and have checked that the files are being loaded.
Upvotes: 0
Views: 846
Reputation: 34340
The problem you have now is that you are extending your class, not including a module into it, so the Sicada::Extensions::UbiquoUser#included
method never gets called.
To fix this, change this line:
UbiquoUser.send(:extend, Sindicada::Extensions::UbiquoUser)
to
UbiquoUser.send(:include, Sindicada::Extensions::UbiquoUser)
Upvotes: 1