Heroku Server Crashing on First API Call?

I haven’t really seen this error before, so I am pretty confused. I created an application with rails, and deployed it to Heroku. I have a table named Lobby, in which multiple lobby objects are stored. The API seems like it is working fine initially, when the Lobby table has no data (i.e it is empty). When I run local, everything works. However, when I try to post my first lobby into my data when its deployed on Heroku, the entire API shows a 500 error. I have tested this with Heroku console many times. I clear all the lobbies, the API works again, I create one lobby, and it crashes. My Heroku logs show the following when I save a lobby:

2018-10-17T21:41:15.558518+00:00 app[api]: Starting process with command bin/rails console by user XXX

2018-10-17T21:41:20.059725+00:00 heroku[run.6598]: Awaiting client

2018-10-17T21:41:20.107283+00:00 heroku[run.6598]: Starting process with command bin/rails console

2018-10-17T21:41:20.197068+00:00 heroku[run.6598]: State changed from starting to up

2018-10-17T21:41:41.779062+00:00 heroku[router]: at=info method=GET path="/lobbies" host=mafia-api.herokuapp.com request_id=0537940a-49b9-43cc-8a5c-1102a3c4e8b4 fwd=“100.40.28.32” dyno=web.1 connect=1ms service=49ms status=500 bytes=203 protocol=https

2018-10-17T21:41:41.734630+00:00 app[web.1]: I, [2018-10-17T21:41:41.734517 #4] INFO – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] Started GET “/lobbies” for 100.40.28.32 at 2018-10-17 21:41:41 +0000

2018-10-17T21:41:41.735300+00:00 app[web.1]: I, [2018-10-17T21:41:41.735222 #4] INFO – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] Processing by LobbiesController#index as HTML

2018-10-17T21:41:41.740013+00:00 app[web.1]: D, [2018-10-17T21:41:41.739927 #4] DEBUG – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] Lobby Load (2.0ms) SELECT “lobbies”.* FROM “lobbies”

2018-10-17T21:41:41.761951+00:00 app[web.1]: D, [2018-10-17T21:41:41.761853 #4] DEBUG – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] No serializer found for resource: #<ActiveModelSerializers::Adapter::Json:0x0000555d25ca5788 @serializer=#<LobbySerializer:0x0000555d25ca57d8 @object=#<Lobby id: 1, name: “please work”, password: “nothing!”, created_at: “2018-10-17 21:41:39”, updated_at: “2018-10-17 21:41:39”, protected: nil>, @instance_options={}, @root=nil, @scope=nil>, @instance_options={}>

2018-10-17T21:41:41.778454+00:00 app[web.1]: I, [2018-10-17T21:41:41.778346 #4] INFO – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] [active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with Array (16.28ms)

2018-10-17T21:41:41.778862+00:00 app[web.1]: I, [2018-10-17T21:41:41.778786 #4] INFO – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] Completed 500 Internal Server Error in 43ms (ActiveRecord: 8.8ms)

2018-10-17T21:41:41.779968+00:00 app[web.1]: F, [2018-10-17T21:41:41.779893 #4] FATAL – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4]

2018-10-17T21:41:41.780143+00:00 app[web.1]: F, [2018-10-17T21:41:41.780075 #4] FATAL – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] NoMethodError (undefined method `username’ for #<Lobby:0x0000555d25d1f060>

2018-10-17T21:41:41.780146+00:00 app[web.1]: Did you mean? users):

2018-10-17T21:41:41.780203+00:00 app[web.1]: F, [2018-10-17T21:41:41.780142 #4] FATAL – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4]

2018-10-17T21:41:41.780273+00:00 app[web.1]: F, [2018-10-17T21:41:41.780211 #4] FATAL – : [0537940a-49b9-43cc-8a5c-1102a3c4e8b4] app/controllers/lobbies_controller.rb:11:in `index’

2018-10-17T21:42:45.185011+00:00 heroku[run.6598]: State changed from up to complete

2018-10-17T21:42:45.166259+00:00 heroku[run.6598]: Process exited with status 0

Ok I see how it is warning me that there is no method ‘username’ for , but I am simply not referencing that anywhere in my code. Here is the Create and Index function that is within my Lobby controller. The index function links to my #index correctly in router and is showing at /lobbies. Again, this all disappears when I create one lobby.

#render a get for all lobbies
def index
 @lobbies = Lobby.all

 if @lobbies
   serialized_data = @lobbies.map { |curLob|
     ActiveModelSerializers::Adapter::Json.new(LobbySerializer.new(curLob))
   }
   render json: serialized_data
 else
   render json: {response: "no lobby data"}
 end
end

#create a new lobby
def create
  #broadcasting out with lobby id so that the channel is unique
  @lobby = Lobby.new(lobby_params)
 serialized_data = ""
 #the second argument here is what is passed via params
  if @lobby.save
    serialized_data = ActiveModelSerializers::Adapter::Json.new(LobbySerializer.new(@lobby))
 end

  render json: serialized_data
end

I have seen a similar post about this on the forum, from 2006, but I’m not sure if it directly applies to my situation, and I don’t believe their solution is working for me.

If this also helps, my models look like this

#/app/models/lobby.rb
class Lobby < ApplicationRecord
  has_many :users
 end

 #/app/models/user.rb
class User < ApplicationRecord
  belongs_to :lobby
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs