0

I'm stuck again.

I have this code in my /views/

<% if DistributionSheet.find(:all, :conditions => ["lifecycle_state = ?","closed"]).last %>
  <% @Specimen_ids.each do |ds| %>
 <th><%= ds.id %></th>
   <% end %>
 </tr>
 <tr>
  <% @all_results.each do |result| %>
   <td><%= result.distribution_sheet_id %></td><td><%= result.lab_id %></td><td><%= result.kit %></td><td><%= result.batch_number %></td><td><%= result.cutoff %></td>
  <% @Specimen_results.each do |sr| %>
  <td><%= sr.result_user %></td>
  <% end %>
  </tr>
   <% end %>
  <%- end -%>
  </tr>

In my controller I have:

    def first


  @Specimen_ids = Specimen.find(:all, :order => 'distribution_sheet_id DESC', :limit => 10)

  @all_results = Result.find(:all, :order => 'distribution_sheet_id DESC', :limit => 1)

  @Specimen_results = SpecimenResult.find(:all, :order => 'id DESC', :limit => 10)

  end
end

What I want is to say only if the LAST DistributionSheet is 'closed' then output the data detailed above ONLY for that closed DistributionSheet.

The above code does output the data, but for all the DistributionSheet. The first if statememnt doesnt seem to be doing anything. I hope I have explained this. If you like more explanation then please tell me. Many thanks in advance.

1 Answer 1

1

You want:

if DistributionSheet.find(:all).last.lifecycle_stated == "closed"
  #code
end

It is displaying all the the DistrubtionSheets because you are grabbing all the records that are "closed", and just checking to see if the last element in that array is not nil/false.

You want the above code because you are getting all the sheets, and then checking if the last one that is retured is infact closed, as stated is the desired result.

Two side notes:

1) You really shouldn't have DB calls in your views.

2) You should really limit your results to something other than :all. This will return a huge dataset if you have lots of records in your DB.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.