Search code examples
ruby-on-railsrabl

Access a child "current" object in rabl


I want to get the object the child node is referering to, so I can make a query.

My code looks like this:

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

The result of this query CourseTypeCategory.where(course_type: course_type, active: true) is always returning the same result as if course_type was always the same each type it is rendered (in this case I suspect is always the first object of @course_types). Is there a way to get the "current object" of the child and make a query like if you were doing a loop (like an each do)?

Thanks in advance and sorry if the question is confusing.


Solution

  • Try this.

    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
    

    Sorry for so little information on this, I'm kind in a hurry.