Reputation: 11607
If I have a "Bill" entity, I might create that bill instance via REST API by assigning the food ordered to the bill when I go to create a new bill.
i.e. I might make a POST request to www.server.com/api/bills with following parameters:
{
cost: 30.0,
foods: [23, 1, 14]
}
Is it better to send an array of ids or is it normal practice to send an array of objects? Like the following:
{
cost: 30.0,
foods: [
{
id: 23,
name: "Chicken Parmesan",
price: 10.0
},
{
id: 1,
name: "Scotch Fillet Steak",
price: 10.0
},
{
id: 14,
name: "Baramundi",
price: 10.0
},
]
}
Upvotes: 1
Views: 532
Reputation: 1576
Yes, sending IDs is perfectly fine, though you want to check that assigned ID are legit.
If you don't have an IDs yet and you want to create objects (or update!) through association – sending a nested array with objects is the way to go.
Also, forgot to mention!
If you are sending just ids – it means you are sending a "special" rails field – food_ids
, and Rails do the magic. That's just an update of a field, that's true REST.
I'd go with food_ids
if I have no need in changing or creating foods, and I'd go with foods_attributes
(nested attributes) if I'd need to.
Upvotes: 3