nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.64k stars 334 forks source link

Child node not printing, pretty stumped #628

Open dmastylo opened 9 years ago

dmastylo commented 9 years ago

My rspec code: screen shot 2015-05-13 at 12 36 01 am

Rsepc result: screen shot 2015-05-13 at 12 36 54 am

As you can see, a team exists for the player in question, but is not being printed in the JSON. On the flip-side, the game node is being printed just fine.

Player/show code: screen shot 2015-05-13 at 12 39 14 am

rabl response

{
  "id": 1,
  "created_at": "2015-05-13T00:34:05.386-04:00",
  "game": {
    "id": 1,
    "name": "Game #<#<Class:0x007fd1f8f3e4c8>:0x007fd1f8f3de38>",
    "starts_at": "2015-05-16T00:34:05.328-04:00",
    "ends_at": "2015-05-16T03:34:05.328-04:00",
    "in_progress": false,
    "players": [
      {
        "player": {
          "id": 1,
          "username": "person28"
        }
      }
    ],
    "teams": [
      {
        "team": {
          "id": 1,
          "name": "990",
          "score": 0,
          "player_count": 1
        }
      },
      {
        "team": {
          "id": 2,
          "name": "469",
          "score": 0,
          "player_count": 0
        }
      }
    ]
  }
}

I have absolutely no idea what's wrong.

nesquena commented 9 years ago

What happens if you do:

node :team do |player|
  team = player.team
   { :id => team.id,  :name => team.name }
end

does that print out as expected? or how about:

child :team => :team do
  attributes :id 
end

anything?

dmastylo commented 9 years ago

Looks like I'm running into even more issues. I tried adding extra attributes to the file but I'm still getting only the old ones??

screen shot 2015-05-13 at 12 52 23 am

{
  "id": 1,
  "created_at": "2015-05-13T00:52:34.307-04:00",
  "game": {
    "id": 1,
    "name": "Game #<#<Class:0x007fe14c5aafe8>:0x007fe14c5aab10>",
    "starts_at": "2015-05-17T00:52:34.232-04:00",
    "ends_at": "2015-05-17T03:52:34.232-04:00",
    "buy_in_price": 19,
    "max_players": 38,
    "in_progress": false,
    "players": [
      {
        "player": {
          "id": 1,
          "username": "person33"
        }
      }
    ],
    "teams": [
      {
        "team": {
          "id": 1,
          "name": "815",
          "score": 0,
          "player_count": 1
        }
      },
      {
        "team": {
          "id": 2,
          "name": "754",
          "score": 0,
          "player_count": 0
        }
      },
      {
        "team": {
          "id": 3,
          "name": "416",
          "score": 0,
          "player_count": 0
        }
      },
      {
        "team": {
          "id": 4,
          "name": "736",
          "score": 0,
          "player_count": 0
        }
      }
    ]
  }
}
dmastylo commented 9 years ago

I believe this might have something to do with namespacing (I have an API::V1 and API::V2 namespace), but this is the first file where I've encountered the issue. My api/v1/games/show and api/v2/games/show have very different setups and they render correctly.

Even weirder... When I do

node :team do |player|
  team = player.team
   { :id => team.id,  :name => team.name }
end

If team is nil (the player doesn't have a team) then an exception is thrown that we're trying to call .id on nil, so it's obviously going through the right file... but the output is still not showing it when the team does exist.

I'm extremely stumped. This isn't happening anywhere else but this file.

dmastylo commented 9 years ago

Okay, so I just tried doing it manually via cURL, everything worked as expected, all the nodes that I put in there worked. Then when I ran rspec, everything passes.

I cannot make this up. Have been pulling my hair out for 3 hours on this. Some sort of weird caching issue or something?? Christ.

nesquena commented 9 years ago

Sorry about this, that does sound frustrating...is everything working now?

dmastylo commented 9 years ago

@nesquena No need to apologize, not your fault. As far as I know everything is working. I don't think this is a rabl issue unless the library is doing some caching under the hood. Really bizarre.