Reputation: 15
I am trying to loop on dates using group by, but I am getting this error.
"Undefined method 'findings' for [Fri, 20 Jan 2017]:Array"
Controller:
def summary
@date = Verif.all
@sub_date = @date.group_by{|d| [d.Submit_Date]}
end
Model:
class Verif < ActiveRecord::Base
def self.findings
Verif.where("Findings = ? or Findings = ?", 'FP','FN').count
end
def self.received
Verif.where.not("App_ID", '').count
end
def self.tp
Verif.where("Findings = ?", 'TP').count
end
end
Views:
<tr id="thead-value">
<td><%= @sub_date.each do |date|%></td>
<% for ds in date %>
<td><%= ds.findings%></td>
<td><%= ds.received%></td>
<td><%= ds.tp%></td>
</tr>
I am very very new to rails. Thank you in advance!
Upvotes: 0
Views: 115
Reputation: 16002
From controller:
def summary
@date = Verif.all
@sub_date = @date.group_by{|d| [d.Submit_Date]}
end
Line:
@sub_date = @date.group_by{|d| [d.Submit_Date]}
will result in this:
#=> {["Fri, 20 Jan 2017"] => [d1, d2], ...}
So when we use it in your view:
@sub_date.each do |date| # date will be the key: ["Fri, 20 Jan 2017"]
Hence the exception for the undefined method.
To solve this, we just need to use key and value in each
like so:
@sub_date.each do |_, date| # date will be the value: [d1, d2... ]
# and key will be ignored because of a "_"
Upvotes: 1