在我的 Rails 应用程序中,我有两个实例变量 @departments 和 @register
@部门=
{
"users": [
{
"departmentid": "DP11"
},
{
"departmentid": "DP12"
},
{
"departmentid": "DP13"
},
{
"departmentid": "DP10"
}
]
}
@注册=
{
"attendance": [
0,
0,
2,
1
]
}
@register 包含一个数组。
是否可以使用 rabl (attendancebydepartment.json.rabl) 视图显示如下格式
{
"users": [
{
"departmentid": "DP11",
"attendance"=0
},
{
"departmentid": "DP12",
"attendance"=0
},
{
"departmentid": "DP13",
"attendance"=2
},
{
"departmentid": "DP10",
"attendance"=1
}
]
}
我的控制器看起来像
def attendancebydepartment
@register = Array.new
@departments = User.select('departmentid').uniq
startdate = params[:startdate]
enddate = params[:enddate]
@count = @departments.count
@departments.each do |d|
@register << (Register.where(:date => startdate..enddate , :departmentid => d.departmentid).sum(:one))+(Register.where(:date => startdate..enddate , :departmentid => d.departmentid).sum(:two))
end
end
是否可以将每个部门及其相应的出勤率添加到数组中,以便我可以显示预期的格式。