In my rails 3.1 application, I have Lists, which have many tasks. All the associations are setup appropriately, I can call aList.tasks, to and I can see the tasks for the List. However, when I load /lists.json, I get the following output:
{
created_at: "2011-12-07T21:51:31Z",
id: 1,
title: "Test 1",
updated_at: "2011-12-07T21:51:31Z"
}
So, the List is serialized to JSON, but its “task” associations are not (tasks are nested under lists, so /list/1/tasks would get the tasks… but just for that 1 list). And if I have 500 lists, I don’t want to have to call 500 times to get all the tasks for each list – I want to get all the tasks when I get all the lists. How can I set this up so the JSON also includes the association? Thoughts?
Thank you!
When rendering a list simply add
include: :tasksintoto_json.P.S. Don’t forget that the new hash syntax only works for ruby >=1.9.2. Here’s the old syntax.