我在我的rails应用程序中遇到此错误no block given (yield)
,这是在我的文件elements = Element.where(study_version_id: self.id).find_each
中引用此行我试图将某些数据导出到XML文件在我的应用程序中生成。当我尝试导出时,我有这个错误,我想帮助解决它。
整个代码相关:
def to_xml
# Build the document and the header elements
odm_document = Odm.new("ODM-#{self.id}", "Assero", "Tourmalet", Version::VERSION)
odm = odm_document.root
study = odm.add_study("S-#{self.id}")
global_variables = study.add_global_variables()
global_variables.add_study_name("#{self.name}")
global_variables.add_study_description("#{self.description}")
global_variables.add_protocol_name("#{self.protocol_name}")
metadata_version = study.add_metadata_version("MDV-#{self.id}", "Metadata for #{self.description}", "Study export.")
protocol = metadata_version.add_protocol()
# Get the set of visits, forms and elements.
visits = Visit.where(study_version_id: self.id).order(ordinal: :asc)
forms = Form.where(study_version_id: self.id).order(ordinal: :asc)
elements = Element.where(study_version_id: self.id).find_each
# Build the Protocol element.
visits.each do |visit|
protocol.add_study_event_ref("SE-#{visit.ordinal}", "#{visit.ordinal}", "Yes", "")
end
# Build the StudyEventDef elements
visits.each do |visit|
study_event_def = metadata_version.add_study_event_def("SE-#{visit.ordinal}", "#{visit.long_name}", "No", "Scheduled", "")
elements = Element.where(study_version_id: self.id, visit_id: visit.id).find_each
elements.each do |element|
#ConsoleLogger.log(C_CLASS_NAME, "to_xml", "Element=#{element.to_json}")
form = Form.find(element.form_id)
study_event_def.add_form_ref("#{form.form_id}", "#{form.ordinal}", "Yes", "")
end
end
# Now the remainder of the metadata
forms.each do |form|
form_json = Mdr.form_to_hash(form.form_id, form.form_namespace)
#ConsoleLogger.log(C_CLASS_NAME, "to_xml", "JSON=#{form_json}")
xml_node(form_json, metadata_version, nil)
end
return odm_document.to_xml
end
答案 0 :(得分:1)
如果您使用的是elements
,则无需将查询结果分配给本地变量find_each
。而是使用块(do |element| ... end
)
Element.where(study_version_id: self.id, visit_id: visit.id).find_each do |element|
#ConsoleLogger.log(C_CLASS_NAME, "to_xml", "Element=#{element.to_json}")
form = Form.find(element.form_id)
study_event_def.add_form_ref("#{form.form_id}", "#{form.ordinal}", "Yes", "")
end
答案 1 :(得分:0)
快速解决方案将find_each
替换为all
。如果您拥有小型数据库(我认为这是您的情况),它将起作用
find_each
用于优化。它不会立即返回所有记录,而是返回记录集(默认为1000)。如果您有1.000.000记录,则会在尝试一次性加载时遇到our of memory
异常。
您可以使用find_each
用法重写代码:
Element.where(study_version_id: self.id, visit_id: visit.id).find_each do |elements|
elements.each do |element|
#ConsoleLogger.log(C_CLASS_NAME, "to_xml", "Element=#{element.to_json}")
form = Form.find(element.form_id)
study_event_def.add_form_ref("#{form.form_id}", "#{form.ordinal}", "Yes", "")
end
end
但我90%肯定你应该选择:
elements = Element.where(study_version_id: self.id, visit_id: visit.id).all
elements.each do |element|
#ConsoleLogger.log(C_CLASS_NAME, "to_xml", "Element=#{element.to_json}")
form = Form.find(element.form_id)
study_event_def.add_form_ref("#{form.form_id}", "#{form.ordinal}", "Yes", "")
end