我为to_xml模型方法编写了一个覆盖,现在我试图将一个哈希属性转换为一个集合,如果符号=>价值观配对,但我没有成功。
intersected_categories
方法的结果是类似哈希的结果:
{:"1"=>["Broken Board", false], :"2"=>["Sticking", false], :"3"=>["Bottom tier missing", false], :"4"=>["Bad I/L", false], :"5"=>["Delamination", false], :"7"=>["Sheet level - smile", false], :"8"=>["Sheet level - frown", false], :"9"=>["Missing SS", false]}
to_xml方法
def to_xml (options={})
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
xml.instruct! unless options[:skip_instruct]
xml.wip @wip,
:number => self.number,
:start_time => self.wip_start_time,
:end_time => self.wip_end_time,
:line => self.system.name,
:shift => self.shift,
:crew => self.crewF,
:sm_crew => self.crewSM,
:sm_shift => self.shiftSM,
:product => self.SMMainGroup,
:product_group => self.product_group,
:sheets => self.sheets,
:interleavers => self.interleavers,
:wip_grade => self.intersected_categories,
:comment => self.comment
end
我尝试了一些没有成功的事情:
def to_xml (options={})
xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
xml.instruct! unless options[:skip_instruct]
@wip.intersected_categories.each do |grade|
wip_categories << {grade[1][0].to_sym => grade[0][1]}
end
.
.
:comment => self.comment,
wip_categories #explodes
end
###### another try
:interleavers => self.interleavers,
:wip_grade => self.intersected_categories,
:comment => self.comment,
self.intersected_categories.each do |grade|
<< grade[1][0].to_sym => grade[0][1] #with / without {} it keeps failing
end
end
我知道如何实现我的需求?
更新
当前xml:
<wip number="123h11008" start_time="2014-05-13 09:21:00 -0500" end_time="" line="cl1" shift="1" crew="A" sm_crew="A" sm_shift="1" product="1111111" product_group="G2" sheets="0" interleavers="0" wip_grade="{:"2"=>["Bottom Tier Missing", false], :"3"=>["Falling", false], :"4"=>["Broken Boards", false], :"5"=>["Crown", false], :"6"=>["DIP", false], :"7"=>["Green Boards", false], :"8"=>["Delamination", false], :"9"=>["Bad I/L", false]}" comment=""/>
预期xml:
<wip number="123h11008" start_time="2014-05-13 09:21:00 -0500" end_time="" line="cl1" shift="1" crew="A" sm_crew="A" sm_shift="1" product="1111111" product_group="G2" sheets="0" interleavers="0" Bottom Tier Missing=false Falling = false Broken Boards = false Crown = false DIP = false Green Boards = false Delamination = false Bad I/L = false comment=""/>
答案 0 :(得分:1)
尝试:
xml.wip @wip, {
:number => self.number,
:start_time => self.wip_start_time,
:end_time => self.wip_end_time,
:line => self.system.name,
:shift => self.shift,
:crew => self.crewF,
:sm_crew => self.crewSM,
:sm_shift => self.shiftSM,
:product => self.SMMainGroup,
:product_group => self.product_group,
:sheets => self.sheets,
:interleavers => self.interleavers,
:comment => self.comment }.merge(Hash(self.intersected_categories.values))