Search code examples
ruby-on-railsrubygrape-api

Undefined method Class `all' in Rails 4 app


I have been following various Ruby on rails REST API tutorials, specifically those using the GRAPE gem. On all these attempts, I have been encountering a undefined method `all' error. I am new to RoR and have been relying on the generators for model generation. Here is the error and code in question:

undefined method `all' for API::V1::EmailAuth:Class

module API
  module V1
  class EmailAuth < Grape::API
    version 'v1' # path-based versioning by default
    format :json # We don't like xml anymore

    resource :email_auth do
      desc "Return list of email_auth"
      get do
        EmailAuth.all # obviously you never want to call #all here
      end
      end
    end
  end
end

The file structure can be seen here: https://i.sstatic.net/1A9oU.jpg (My rep is too low to post images)

This is my first time actually posting something in stackoverflow, do let me know if I can refine the question.

Just a note: I have refered to Undefined method `all' for ActiveSupport in Rails 4.0.0 app and various similar questions regarding namespace issues, but can seem to resolve this issue.

Thanks!


Solution

  • If you have a class Grape::API::EmailAuth and in this class you want to load the active record class EmailAuth, then you need to reference it by two colons:

    get do
      ::EmailAuth.all
    end