Suggestion to reduce number of junk hierarchical 'if loops'

Hi,

I have hierarchical “if loops” like this

<% @descriptions.each_with_index do |description, i| %>
<% description.tale2.each do |tax_ref| %>
<% if condition %>
<% if condition %>
<% if condition %>
<%= $text_first_describe%> <%= $paren_author_yr %>
<% ref_sp_uniq.each_with_index do |ref, i| %>
<% if ref == tax_ref.ref_wo_brace%>
<% execution %>
<% elsif i == (ref_sp_uniq.size - 1)%>
<%# @ref_desc = “#{@ref_desc_numb}. #{tax_ref.ref_wo_brace}” %>
<% end %>
<% end %>
<% if condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% elsif taxon_name.emend_author_year %>
<%= print %>
<% else %>
<%= print %>
<% end %>
<% end %>
<% else %>
<% if condition %>
<%= print %>
<% ref_sp_uniq.each_with_index do |ref, i| %>
<% if condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% end %>
<% end %>
<% if condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% else %>
<% execution %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>

Kindly suggest me possible way to reduce this kind of junk “if loops”.

Hi,

I have hierarchical “if loops” like this

<% @descriptions.each_with_index do |description, i| %>
<% description.tale2.each do |tax_ref| %>
<% if condition %>
<% if condition %>
<% if condition %>
<%= $text_first_describe%> <%= $paren_author_yr %>
<% ref_sp_uniq.each_with_index do |ref, i| %>
<% if ref == tax_ref.ref_wo_brace%>
<% execution %>
<% elsif i == (ref_sp_uniq.size - 1)%>
<%# @ref_desc = “#{@ref_desc_numb}. #{tax_ref.ref_wo_brace}” %>
<% end %>
<% end %>
<% if condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% elsif taxon_name.emend_author_year %>
<%= print %>
<% else %>
<%= print %>
<% end %>
<% end %>
<% else %>
<% if condition %>
<%= print %>
<% ref_sp_uniq.each_with_index do |ref, i| %>
<% if condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% end %>
<% end %>
<% if condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% elsif condition %>
<% execution %>
<% else %>
<% execution %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>

Hi Palani… first thing is find a way to move your logic to the Model. Your html.erb code really should be as much as possible just to present your data. Secondly, using the ‘case’ construction might help:

a = 1
case somthing
when ‘x’:
do somthing
when ‘y’
do something else
when ‘z’
do another thing…

end

Hi david,

thanks for your suggestion.

I tried ‘case’ statement before. But, I am using ‘if’ statements in the array loops of @descriptions and description.table2. @descriptions is an array generated in table 1’s controller. If I wish to ‘use’ either table 1 or table 2 models… I am getting into problem to merge this two array loops. how i can use @desctipion (array generated in table1 controller) in the model of table 2? Kindly help this regard.

PalaniKannan K wrote in post #978680:

You have a few paths which will yield nothing (no executions, no
output).

if !condition1 - nothing occurs
if condition1 and condition2 and !condition3 - nothing occurs
if condition1 and !condition2 and !condition6 - nothing occurs

Scope your data access (either description or tale2) to eliminate those
cases from the data considered, then simplify your logic.

Less data to paw through with the rest of the code, and case might be a
better construct for readability.

BTW, what are all those execution steps?

Dear Ar Chron,

these are hierarchial conditions. so i need in hierarchial lever… the conditions as well as execution content are “columns of @description from table1 and description.tale2”.

Hi david,

thanks for your suggestion.

I tried ‘case’ statement before. But, I am using ‘if’ statements in the array loops of @descriptions and description.table2. @descriptions is an array generated in table 1’s controller. If I wish to ‘use’ either table 1 or table 2 models… I am getting into problem to merge this two array loops. how i can use @desctipion (array generated in table1 controller) in the model of table 2? Kindly help this regard.

I would think also you might be able to use includes or joins in your activerecord call, and/or a where condition to ‘merge’ your results. Personally I would write out in pseudo-code what trying to do and then get as close to the db first (using activerecord in this case).

Hi,

I have hierarchical "if loops" like this

[snip]

Kindly suggest me possible way to reduce this kind of junk "if loops".

Is there a reason you're not using helpers and/or partials? They're
the standard Rails method of reducing complexity in views...

--Matt Jones