I'm currently having a hard time finding a solution for a problem I have in a Ruby on Rails project.
I have the following model structure:
class Room < ActiveRecord::Base
has_many :photos
end
class Photo < ActiveRecord::Base
belongs_to :room
has_many :points
end
class Point < ActiveRecord::Base
belongs_to :photo
end
I want to query the database so that I get something that rendered as JSON would have this structure:
{
"rooms" :
[
//room 1
{
"id": 1,
"param1": "dummy",
"photos":
[
{
"id": 3,
"title": "test photo",
"points":
[
{"id": 1, "text": "test point"},
{"id": 2, "text": "test point"}
]
}
]
},
//room 2
{
"id": 2,
// ...
}
]
}
Basically, I want to make a query that returns all the info, in all the models, structured according to the models relations.
I searched for a couple of hours and can't find any simple solution for this... (not even complicated one). I don't know if I'm not using the correct search keywords for my problem, but I really couldn't find anything that works...
For a partial solution (to get only rooms and photos), I tried this:
Room.all.includes(:photos)
From what I found, I thought it could be the solution, but it just returns the information about the rooms, not the photos, so probably this is a totally different approach to the one I need.