簡體   English   中英

訪問rabl中的子“當前”對象

[英]Access a child “current” object in 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 )?

在此先感謝您,如果問題令人困惑,我們將深表歉意。

嘗試這個。

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

抱歉,您所獲得的信息太少了,我很着急。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM