rails更改循环内的布尔值

时间:2016-05-11 18:35:57

标签: ruby-on-rails ruby ruby-on-rails-4

大家好我需要帮助这个方法: 我有一个循环通过两个模型的数据并比较它们的方法;如果找到匹配项,我想将 nih_pub 匹配属性设置为true,然后返回匹配为false的所有nih_pub。 如果我使用注释的if语句if nih.pubyear == "2003" and nih.pmid == "12538806",它会很好,但是它不能用于循环。

下面是我正在使用的方法:

def compare
  nih_pub = NihPublication.where(user: current_user).all
  pubmed_pub = Publication.where(user: current_user).all

  nih_pub.each{ |nih|
    pubmed_pub.each {|pubmed|
      if nih.pubyear == pubmed.publication_year and  nih.pmid == pubmed.pubmed_id
      # if nih.pubyear == "2003" and  nih.pmid == "12538806"
        nih.match = true
        nih.save!
      end
    }
  }

  @missing = nih_pub.where(match: false)
end

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这可能是数据类型问题。您可以在控制台中比较数据类型,如下所示:

get-content $file -ReadCount 3000 |
 ForEach-Object {
  if ($_ -like '*warn*')
    {$Lastfound = $_}
 }

 ($Lastfound -like '*warn*')[-1]

如果你看到这样的话:

def compare
  nih_pub = NihPublication.where(user: current_user).all
  pubmed_pub = Publication.where(user: current_user).all

  nih_pub.each{ |nih|
    pubmed_pub.each { |pubmed|
      puts "nih.pubyear #{nih.pubyear.class.name}" 
      puts "pubmed.publication_year #{pubmed.publication_year.class.name}"
      puts "nih.pmid #{nih.pmid.class.name}"
      puts "pubmed.pubmed_id #{pubmed.pubmed_id.class.name}"

      if nih.pubyear == pubmed.publication_year and nih.pmid == pubmed.pubmed_id
      # if nih.pubyear == "2003" and  nih.pmid == "12538806"
        nih.match = true
        nih.save!
      end
    }
  }

  @missing = nih_pub.where(match: false)
end

然后你知道在比较它们时你需要施放值。

我怀疑您需要将代码更改为类似的内容(尽管这只是猜测,并且基于我上面列出的调试输出):

nih.pubyear Integer
pubmed.publication_year String
nih.pmid Integer
pubmed.pubmed_id String