0

Currently my JSON request is returning the below, where each person/lender has many inventories.

#output of /test.json

[
  {"id":13, "email":"johndoe@example.com", "inventories":
    [
      {"id":10,"name":"2-Person Tent","category":"Camping"},
      {"id":11,"name":"Sleeping bag","category":"Camping"},
      {"id":27,"name":"6-Person Tent","category":"Camping"}
    ]
  },

  {"id":14, "email":"janedoe@example.com", "inventories":
    [
      {"id":30,"name":"Electric drill","category":"Tools"},
      {"id":1,"name":"Hammer","category":"Tools"},
      {"id":37,"name":"Plane","category":"Tools"}
    ]
  }
]

I need to nest in one more thing and am having trouble doing so. For context, each inventory item is referenced via it's id as a foreign key in a borrow record. Each borrow record belongs to a request parent that stores returndate and pickupdate. What I need now, is for each inventory item, to nest an array of all the request records, with information on pickupdate and returndate. In other words, desired output:

[
  {"id":13, "email":"johndoe@example.com", "inventories":
    [
      {"id":10,"name":"2-Person Tent","category":"Camping", "requests": 
        [
          {"id":1, "pickupdate":"2014-07-07","returndate":"2014-07-10"},
          {"id":2, "pickupdate":"2014-06-02","returndate":"2014-06-05"},
          {"id":3, "pickupdate":"2014-08-14","returndate":"2014-08-20"}
        ]
      },
      {"id":11,"name":"Sleeping bag","category":"Camping", "requests":
        [
          {"id":4, "pickupdate":"2014-05-27","returndate":"2014-05-30"},
          {"id":5, "pickupdate":"2014-04-22","returndate":"2014-04-25"}
        ]
      },
      {"id":27,"name":"6-Person Tent","category":"Camping", "requests":
        [
          {"id":6, "pickupdate":"2014-07-10","returndate":"2014-07-12"}
        ]
      }
    ]
  },

  {"id":14, "email":"janedoe@example.com", "inventories":
  ...

I have written the following code:

json.array!(@lenders) do |json, lender|
  json.(lender, :id, :email)
  json.inventories lender.inventories do |json, inventory|
    json.id inventory.id
    json.name Itemlist.find_by_id(inventory.itemlist_id).name

    #code below says, json.requests should equal all the Requests where there is a Borrows within that Request that is using the Inventory in question
    json.requests Request.select { |r| r.borrows.select { |b| b.inventory_id == inventory.id }.present? } do |json, request|
      json.pickupdate request.pickupdate
      json.returndate request.returndate
    end
  end
end

When I refresh the page, I get wrong number of arguments (0 for 2..5)

I feel like the issue is that the Request.select... is returning an Array which isn't what needs to go here... but in the earlier nested function lender.inventories is an Inventory::ActiveRecord_Associations_CollectionProxy though I'm not sure how to correct for this.

NOTE: Someone said the problem could be that unlike with the nesting between inventories and lender, there's not an explicit association between inventory and request, but then again the line json.name Itemlist.find_by_id(inventory.itemlist_id).name worked so I'm not sure this is right. (Also if this is the case, I'm not sure how to bypass this limitation... I currently don't want to create a relationship between the two.)

Thanks!

james
  • 3,989
  • 8
  • 47
  • 102

1 Answers1

0

ARG. Ok so this code is perfectly right. The issue was that I"m using the Gon gem in conjunction with Jbuilder, and Request is a predefined class in Gon.

So just changed code to

@requestrecords.select....

And in the controller:

@requestrecords = Request.all

-__-

james
  • 3,989
  • 8
  • 47
  • 102