I have a model that extends ActiveRecord::Base
and includes a concern:
class User < ActiveRecord::Base
include UserConcern
def self.create_user()
...
results = some_method()
end
end
UserConcern
is stored in the concerns
directory:
module UserConcern
extend ActiveSupport::Concern
def some_method()
...
end
end
I am getting a run-time error when I try to create a new user by calling the create_user
method that looks like this:
undefined method 'some_method' for #<Class:0x000000...>
I have two questions about this:
Why is the
some_method
undefined? It seems to me that I am properly including it with the statementinclude UserConcern
. Does it have something to do with myUser
class extendingActiveRecord::Base
? Or maybe something to do with the fact that I am callingsome_methods()
from a class method (i.e.self.create_user()
)?Why does the run-time error refer to
#<Class:0x000000...>
instead of to#<User:0x000000...>
?
#<Class
because the classUser
is an instance of the classClass
. - nbermudezssome_method
depends on an instance or not, if it doesn't make it a class method and everything should work as you expect. If it does, then you need to rethink why you are calling it inside a class method. Maybe you expect an instance as an argument? - nbermudezscreate_user
does what it name indicates then you probably take a look at the before and after create callbacks to do something when a new instance is created. - nbermudezs