2016-10-02 50 views
0

我想獲取子節點引用的對象,因此我可以進行查詢。在rabl中訪問兒童「當前」對象

我的代碼如下所示:

child @course_types => :course_types do |course_type| 
    attributes :id, :name, :deleted 
    child CourseTypeCategory.where(course_type: course_type, active: true) => :category_position do 
    attributes :category_id, :position 
    end 
end 

此查詢CourseTypeCategory.where(course_type: course_type, active: true)總是返回相同的結果,如果course_type總是相同的各類型它就會出現(在這種情況下,我懷疑的結果總是@course_types的第一個對象)。有沒有辦法讓孩子的「當前對象」,並作出查詢,如果你正在做一個循環(如each do)?

如果問題很混亂,請提前致謝。

回答

1

試試這個。

child @course_types => :course_type do 
    attributes :id, :name, :deleted 
    node(:course_type_category) do |course_type| 
     CourseTypeCategory.where(course_type: course_type, active: true).collect do |category| 
      {category_id: category.id, position: category.position } 
     end 
    end 
end 

對不起,我很着急。

+0

工作感謝 – InesM