Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道:未给出块(屈服)_Ruby On Rails - Fatal编程技术网

Ruby on rails 轨道:未给出块(屈服)

Ruby on rails 轨道:未给出块(屈服),ruby-on-rails,Ruby On Rails,我在我的rails应用程序中遇到了这个错误no block-gived(yield),这是指我的文件elements=Element.where(study\u version\u id:self.id)中的这一行。查找每个我试图将在我的应用程序中生成的一些数据导出到XML文件中。当我尝试导出时,我遇到了这个错误,我希望得到一些帮助来解决它 整个代码与以下内容相关: def to_xml # Build the document and the header elements o

我在我的rails应用程序中遇到了这个错误
no block-gived(yield)
,这是指我的文件
elements=Element.where(study\u version\u id:self.id)中的这一行。查找每个
我试图将在我的应用程序中生成的一些数据导出到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

快速溶剂化是用
all
替换
find_each
。如果你有一个小的数据库,它就会工作(我想这是你的情况)

find_每个
都用于优化。它一次返回的不是所有记录,而是记录集(默认为1000)。如果您有1.000.000条记录,您将获得
我们的内存
异常,以防试图同时加载所有记录

您可以使用
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

如果使用的是
find\u each
,则无需将查询结果分配给局部变量
elements
。而是使用块(
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