Description
Bit of a unique use case here - we work in education, and thus have Student
and Klass
models and corresponding relationships. The call student.klass
returns that student's "class" (meaning their classroom, not the programming class. I know - confusing). When nesting an entity inside of Student as detailed in the docs -
def entity
Entity.new(self)
end
class Entity < Grape::Entity
expose :name, :student_code
end
and using present Student[:id]
it fails with the error NoMethodError: undefined method 'ancestors' for #<Klass:0x007f9b734bbe30>
. asdfa
Further digging led me to find that the entity_class_for_obj
method inside lib/grape/dsl/inside_route.rb
is to blame. More specifically, the condition at line #360 - object.respond_to?(:klass)
Technically, my class Student
responds to this, but it returns an actual Klass
object. This can be fixed by using with: Student::Entity
but I would have to use that everywhere we return a student, which in our case would be a giant pain.
I was able to fix this by modifying the if statement to if object.respond_to?(:klass) && object.klass.class != Klass
. I'm not sure if this is something that you'd want to add to the project since it is admittedly a pretty unique use case, but I'm not entirely sure why the check for .klass is there either. Nonetheless, any model that has a method 'klass' would presumably fail in this scenario. I couldn't see any unforeseen side effects other than having to deal with the added condition and associated object.klass.class != Klass
word vomit...