user3326661
user3326661

Reputation: 1

Ruby on Rails not connecting to Table

I'm using Ruby on Rails 4.0.0.

Over in app/controllers/api/v1/glossary_controller.rb:

class Api::V1::GlossaryController < ApplicationController
    def index
        render json: Glossary.all
    end
end

And in app/models/api/v1/glossary.rb:

class Api::V1::Glossary < ActiveRecord::Base
    table_name "glossary"

    @glossary = self.all
end

As well as app/models/api/v1.rb

module Api::V1
  def self.table_name_prefix
    'api_v1_'
  end
end

The route is as so:

Feta::Application.routes.draw do

  namespace :api, defaults: {format: 'json'} do
    namespace :v1 do
        get "glossary" => "glossary#index"
    end
  end

end

And the DB table is named "api_v1_glossary".

And the error: uninitialized constant Api::V1::Glossary::Glossary from glossary.rb in models when I define @glossary...

EDIT: I've updated from meager, but the problem of getting it to use glossary instead of glossaries persists. @glossay.to_yaml returns nothing. @glossary comes back nil.

Upvotes: 0

Views: 70

Answers (2)

krabbi
krabbi

Reputation: 135

According to RailsGuides you have to set the table name with self.table_name = 'glossary' but I am not sure if that is enough in the given example.

Upvotes: 0

user229044
user229044

Reputation: 239270

I'm not sure what that @glossary line is supposed to do, but you need to refer to either self or Api::V1::Glossary there.

class Api::V1::Glossary < ActiveRecord::Base
    table_name = "glossary"

    @glossary = self.all
end

Upvotes: 1

Related Questions