我有一个Staff模型如下(为简洁起见)
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')
所以我现在按照员工姓名和日期排序特定周的所有StaffTimePeriod。
我需要为特定周生成XML文件,以下列格式显示员工姓名,日期,staffstatus。
<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.firstname + lastname和Staff.stafftype更改Staff.id
这是我的构建器代码:
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标记,它表示这是一个Starting XML标记,也是一个Ending XML标记,可以独立创建,而不是在do ... end
循环中创建。
我可以独立于&#39; do end&#39;定义单独的起始和结束XML标记。环?如果没有,那么我该如何实现上述目标?