Вопрос

I have the two models as seen below. An instance of Share may have multiple instances of Color. I'm using MongoMapper to manage these models. When I do Share.create, I'm getting the following error:

NameError: uninitialized constant Color

Can anyone tell me why this is?

/models/share.rb:

class Share
  include MongoMapper::Document

  key :shorten_id, String
  key :name, String
  many :colors, :class_name => "Color"
  timestamps!
end

/models/color.rb:

class Color
   include MongoMapper::Document

   key :celcius, Float
   key :hue, Float
   key :saturation, Float
   key :brightness, Float
   belongs_to :share, :class_name => "Share"
   timestamps!
end

This is where I try to create the instances:

/routes/api.rb:

require 'json'

class App < Sinatra::Base
  register Sinatra::Namespace

  namespace '/api' do
    before do
      protected!
    end

    get '/shares' do
      content_type 'application/json'
      Share.all.to_json
    end

    post '/share' do
      @share = Share.create
      @share.save
    end
  end
end
Это было полезно?

Решение

It turned out that I could not have a model named Color. I guess it's a reserved name. Changing it to ShareColor solved the issue.

Другие советы

Since you are using Sinatra, you need to load all the models you use manually via a require statement. require loads a file and executes all its statements, but also makes sure the same file is not loaded twice.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top