Reputation: 6253
currently I would like to print a report with many details, what I did currently is like sample code below
@report = Report.first
@details = @report.details
if @details
# I'm using this code to check not nil
if @details.count > 0
# if it's not nil then I check if there is a record
# print detail table
end
end
if I'm looking better way to check not nil and the record count is bigger than zero.
Upvotes: 0
Views: 185
Reputation: 102055
any?()
Returns true if there are any records.
@report = Report.includes(:details).first
@details = @report.details
if @details.any?
# ...
end
Upvotes: 2
Reputation: 106440
Depending on the version of Rails you're running, you can use the try
method instead.
if @report.try(:details).try(:any?)
# logic
end
Upvotes: 1
Reputation: 76
Perhaps something as the following:
if @report.details.present?
#rest of code
end
Any questions ask below
Hope it helps!
Upvotes: 0