Rails 3.0.4
Given the following relationship
class Child < ActiveRecord::Base
belongs_to :parent
end
class Parent < ActiveRecord::Base
has_many :children
end
Using
@parents = Parent.find(:all, :include => :children)
Will return every parent with its children.
How can each child also include a reference to its parent so that can be used during serialisation?
E.g. in a JSON format that would look like:
[
{
"parent": {
"created_at": "2012-05-05T11:29:19Z",
"id": 1,
"updated_at": "2012-05-05T11:29:19Z",
"children": [
{
"created_at": "2012-05-05T11:35:05Z",
"id": 1,
"updated_at": "2012-05-05T11:35:05Z",
"parent": {
"created_at": "2012-05-05T11:29:19Z",
"id": 1,
"updated_at": "2012-05-05T11:29:19Z"
}
}
]
}
}
]
1 Answer