1
@test = Test.find(params[:test_id].to_i)
group_tests_id = @test.group_tests.map(&:group_id)
@std = Student.joins(:group_students).where("group_id IN (?)",group_tests_id).uniq
render :json => {:students => @std.as_json(include: [:user.as_json(only: [:id, :first_name])]), :test => @test.as_json()}
每件事情都可以正常工作。在渲染JSON期間嵌套包含不能在Rails 4中工作
我得到這個結果
{
"students": [
{
"id": 38,
"user_id": 34,
"created_at": "2017-08-07T12:30:02.120+05:00",
"updated_at": "2017-08-07T12:30:02.120+05:00",
"user": {
"id": 34,
"username": "taimoor123",
"created_at": "2017-08-07T12:30:02.067+05:00",
"updated_at": "2017-08-07T12:33:19.273+05:00",
"first_name": "Muhammad Taimoor",
"second_name": "Sultani",
"school_name": "Moon Public School",
"section_name": "Blue 10th",
"gender": "Male",
"age": 23,
"role": "Student",
"school_id": 1,
"section_id": 2,
}
}
],
"test": {
"id": 33,
"name": "Test 8/7/2017",
"attempt_time": 15,
"teacher_id": 1
}
}
但我只是想user
id
和first_name
在student
對象。 我很困惑,爲什麼嵌套include不在這裏工作。 您的幫助將不勝感激。 在此先感謝。
我的錯誤。謝謝你的幫助。 – Taimoor