我有一个员工模型如下(为简洁起见)
id
firstname
lastname
stafftype (e.g. Chef, Driver, Team Leader)
我也有一个 StaffTimePeriod 模型如下
id
staffid
date
staffstatus (e.g. Working, Holiday, Sick)
每个员工每天都有一个 StaffTimePeriod 记录。
我有正确的关联设置,允许我执行以下操作:
@stps = StaffTimePeriod.includes(:staff).where('date >= ? and date <= ?',begofweek,endofweek).order('staff.firstname,staff.lastname,date')
因此,我现在得到了按员工姓名和日期排序的特定周的所有 StaffTimePeriods。
我需要为特定周生成一个 XML 文件,以下列格式显示员工姓名、日期、员工状态。
<rows>
<row>
<cell>firstname+lastname</cell>
<cell>stafftype</cell>
<cell>date (for Monday )</cell>
<cell>staffstatus (for Monday)</cell>
<cell>date (for Tue )</cell>
<cell>staffstatus (for Tue )</cell>
<cell>date (for Wed)</cell>
<cell>staffstatus (for Wed)</cell>
<cell>date (for Thu )</cell>
<cell>staffstatus (for Thu)</cell>
<cell>date (for Fri)</cell>
<cell>staffstatus (for Fri)</cell>
<cell>date (for Sat)</cell>
<cell>staffstatus (for Sat)</cell>
<cell>date (for Sun)</cell>
<cell>staffstatus (for Sun)</cell>
</row>
<row>
<cell>firstname+lastname (Next Staff)</cell>
....
....
</row>
</rows>
在 Builder 中,我试图迭代 @stps 但仅在 Staff.id 更改时输出 Staff.firstname+lastname 和 Staff.stafftype
这是我的生成器代码:
xml.instruct! :xml, :version => "1.0"
xml.tag!("rows") do
tmpstaffid = nil
weekdays = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
@stps.each do |stp|
if tmpstaffid != nil && tmpstaffid != stp.staff.id
xml.tag!("/row")
end
if tmpstaffid != stp.staff.id
xml.tag!("row", {"id" => stp.staff.id})
xml.tag!("cell", stp.staff.fullname)
xml.tag!("cell", stp.staff.stafftype)
tmpstaffid = stp.staff.id
end
if weekdays.include?(stp.stpdate.strftime('%a'))
xml.tag!("cell", stp.stpstatus)
else
xml.tag!('cell','')
end
end
end
但是,这给了我以下输出:
<rows>
<row id="4"/>
<cell>Fname4 Lname4</cell>
<cell>4</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
<cell>79</cell>
</row/>
<row id="5" />
.....
</row/>
</rows>
正如您所看到的,开始'<row>'
也包含一个终止/>
。
我需要的是一个 XML 标记,它表示这是一个起始 XML 标记,也是一个结束 XML 标记,可以独立创建而不是在do ... end
循环中创建。
我可以独立于“do end”循环定义单独的开始和结束 XML 标记吗?如果没有,那么我该如何实现上述目标?