ruby - ActiveModel::Serializer instance variable in class unaccessible in method -
i have snippet below in activemodel::serializer
. it's recursive method, need add array that's returned when recursion ends.
so, set instance variable @rows
equal empty array []
. push onto array in method, ruby returning undefined method 'push' nil:nilclass
.
class myserializer < activemodel::serializer attributes :hierarchies @rows = [] def hierarchies_run(child, user) hierarchy = userhierarchy.where(child: child, user_id: user).first if hierarchy @rows.push(usersimpleserializer.new(user.find(hierarchy.child), :root => false)) hierarchies_run(hierarchy.parent, current.id) else @rows.push(usersimpleserializer.new(user.find(child), :root => false)) end end def hierarchies hierarchies_run(current.id, current.id) @rows end end
why returning error?
the problem have defined @row
in class (the constructor itself) instead of instance. should put initialization of @row
like:
class myserializer < activemodel::serializer attributes :hierarchies def initialize @rows = [] end ...
then when call
myserializer.new
@row
initialized within instance's context
Comments
Post a Comment