ruby on rails - Undefined method in ActiveSupport concern -
i have model extends activerecord::base
, includes concern:
class user < activerecord::base include userconcern def self.create_user() ... results = some_method() end end
userconcern
stored in concerns
directory:
module userconcern extend activesupport::concern def some_method() ... end end
i getting run-time error when try create new user calling create_user
method looks this:
undefined method 'some_method' #<class:0x000000...>
i have 2 questions this:
why
some_method
undefined? seems me including statementinclude userconcern
. haveuser
class extendingactiverecord::base
? or maybe fact callingsome_methods()
class method (i.e.self.create_user()
)?why run-time error refer
#<class:0x000000...>
instead of#<user:0x000000...>
?
try it
models/concerns/user_concern.rb:
module userconcern extend activesupport::concern def some_instance_method 'some_instance_method' end included def self.some_class_method 'some_class_method' end end end
models/user.rb:
class user < activerecord::base include userconcern def self.create_user self.some_class_method end end
rails console:
user = user.new user.some_instance_method # => "some_instance_method" user.some_class_method # => "some_class_method" user.create_user # => "some_class_method"
http://api.rubyonrails.org/classes/activesupport/concern.html
Comments
Post a Comment