Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity population fails with a relation named Klass #317

Open
jaman1020 opened this issue Mar 10, 2019 · 0 comments
Open

Entity population fails with a relation named Klass #317

jaman1020 opened this issue Mar 10, 2019 · 0 comments

Comments

@jaman1020
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant