我正在使用 Rails 查询数据并将其放入哈希中,就像这样......
class AssignmentsController < ApplicationController
respond_to :json
def index
student = Student.find(current_user.student_id)
@assignments = Hash.new
@assignments["individual"] = Assignment.where(:student_id => student.id)
unless student.group_lesson_ids.nil?
student.group_lesson_ids.each do |g|
group_lesson = GroupLesson.find(g)
@assignments[group_lesson.name] = Assignment.where(:group_lesson_id => g)
end
end
end
end
然后我希望 Rabl 将其转换为 JSON 以供 Marionette 应用程序使用。
这是 Rabl 文件
object @assignments
attributes :id, :title, :student_id, :assigned
但是当我在浏览器中检查 JSON 时,它只显示了 ActiveRecord 关系。
{
"#<ActiveRecord::Relation::ActiveRecord_Relation_Assignment:0x007fa2956b43a8>": [
{ },
{ },
{ }
]
}
我知道这是因为延迟加载的概念,但是在这种情况下我应该怎么做才能使 JSON 对 Marionette 可用?