簡體   English   中英

用戶只輸入一次結果

[英]users enter results only once

我的結果控制器中有以下腳本:

 distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","open"]).last


   if distribution_sheet.nil?
     redirect_to root_path  #you could redirect somewhere else if you want
      flash[:notice] = "There are currently no active EQAs"
  # elsif (@result.size > 1)
  #   redirect_to root_path  #you could redirect somewhere else if you want
   #   flash[:notice] = "You have already entered your EQA for EQA number #{distribution_sheet.id}"
    else
    flash[:notice] =      "EQA number #{distribution_sheet.id} is open for submissions"
  end

結果是一個模型。 當DistributionSheet僅“打開”一次時,我需要用戶輸入新結果。 上面的elsif似乎不起作用。 有什么建議嗎?

這是@result被引用的地方:

    def new
   @result = Result.new
   distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","open"]).last
   @result.distribution_sheet_id = distribution_sheet.id
  10.times do
            @result.specimen_results.build

  end
  specimen_ids  = distribution_sheet.specimens.collect{|specimen| specimen.id}
  @result.specimen_results.each do |specimen_result|
        specimen_result.specimen_id = specimen_ids.shift
  end

   @result.lab_id = current_user.lab_id

  end

有幾件事。 通過做這個:

 distribution_sheet = DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","open"]).last

您正在加載數據庫中滿足這些條件的每個單個DistributionSheet對象。 現在似乎很快,但是當您有大量記錄時,將需要一段時間。 最好這樣做:

distribution_sheet = DistributionSheet.first(:conditions => "lifecycle_state = 'open'", :order => "id DESC")

這將實現相同的目標,但首先不會加載整個關聯,僅加載您需要的一條記錄。

其次,也許不是檢查@result.size > 1而是應該在關聯上檢查它:

distribution_sheet.results.count > 1

這將執行SQL查詢以計算distribution_sheet對象的results數。 您首先需要在DistributionSheet模型中具有has_many :results

還有兩件事:我建議閱讀入門指南以及協會基礎知識,以了解正確解決此問題所需的基礎知識。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM