I am have student table with fields student_id, name and course_id. I need to fetch all records in the table as json response in Ruby on Rails. I need the json in the format
{"records" : [{ "course" : 1 #course id in the table
"students" : [ {
"id" : 5
"name" : "Ragav"
},
{
"id":6,
"name": "Rohith"
}]
},
{ "course":2,
"students" : [ {
"id" : 8
"name" : "Ragav"
},
{
"id":10,
"name": "Rohith"
}]
}
]}
I am doing this using JBuilder like this
json = Jbuilder.new do |j|
j.id student_id
j.name name
end
in as_json method in model. I am new in RoR. Please help