2016-10-02 52 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