Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate through an array in rails view?

I did a query in MySql but is working in Rails and mysql2 gem.

Here is the information:

http://sqlfiddle.com/#!2/9adb8/6

The query is working fine without problems and showing this result:

 UNIT  V1  A1  N1   V2 A2  N2   V3  A3  N3   V4  A4  N4   V5  A5  N5
 LIFE  2   0   0    1   2  0    0   0   0     0   0   0   0    0   0
 ROB   0   1   0    0   1  2    0   0   0     0   0   0   0    0   0 

-Installed mysql2 gem for rails 2.3.8

gem install mysql2 -v0.2.6

-Created the controller:

class PolicyController < ApplicationController

   def index
      @result =  ActiveRecord::Base.connection.execute("select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id  INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1  Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);")
      @result2 = ActiveRecord::Base.connection.execute("prepare stmt from @sql;")
      @result3 = ActiveRecord::Base.connection.execute("execute stmt;")
   end

end

Here is the log:

SQL (0.9ms)   select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1 Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);
SQL (0.9ms)   prepare stmt from @sql;
SQL (0.2ms)   execute stmt;

Here is the view (is working fine and without problems but seems to be too long write several times the same code)

<table>
  <% @result3.each do |policy| %>
  <tr>
      <td><%= policy[0] %></td>
      <td><%= policy[1] %></td>
      <td><%= policy[2] %></td>
      <td><%= policy[3] %></td>
      <td><%= policy[4] %></td>
      <td><%= policy[5] %></td>
      ...
  </tr>
  <%end%> 
</table>

I tried to use inspect but it shows all the information in one td and not on each td:

<% @result3.each do |policy| %>
<tr>      
 <td align="center"><%= policy.inspect %></td>
</tr>
<%end%> 

How can I do to show all this without writing lots of lines?

Is it possible to make this in one line? without writing <%= policy[#NUMBER] %>

Please somebody can help me with this?

I will really appreciate it.

like image 868
Carlos Morales Avatar asked Apr 11 '14 05:04

Carlos Morales


People also ask

How do I iterate an array in Ruby on Rails?

The Ruby Enumerable#each method is the most simplistic and popular way to iterate individual items in an array. It accepts two arguments: the first being an enumerable list, and the second being a block. It takes each element in the provided list and executes the block, taking the current item as a parameter.

How do you iterate through an array?

Iterating over an array You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.

Which method is used to iterate over arrays and objects?

Lodash forEach() to Iterate over Objects Lodash has a helpful iteration methods, such as forEach and map that work on objects as well as arrays.

How can a for loop be used to iterate through an array?

For performing operations on arrays, the need arises to iterate through it. A for loop is used to iterate over data structures in programming languages. It can be used here in the following ways: Explanation: The variable i is initialized as 0 and is defined to increase at every iteration until it reaches the value of the length of the array.

What is a rails view?

A Rails View is an ERb program that shares data with controllers through mutually accessible variables. If you look in the app/views directory of the library application, you will see one subdirectory for each of the controllers, we have created: book.

How to iterate over an array in react?

Now that you know how a map function works, let’s get back to how you can iterate over an array in React. Inside a React component, you can use the map function to iterate over an array of data and return a JSX element that renders that will be rendered by React.

How to loop through an array in JavaScript?

The for...of Loop iterates over iterable objects such as arrays, sets, maps, strings, and so on. It has the same syntax as the for...in loop, but instead of getting the key, it gets the element itself. This is one of the easiest methods for looping through an array and was introduced in later versions of JavaScript ES6.


2 Answers

Hmmm, might have missunderstood your question since it looks really simple, are you trying to shorten this?

Change:

<td><%= policy[0] %></td>
<td><%= policy[1] %></td>
<td><%= policy[2] %></td>
<td><%= policy[3] %></td>
<td><%= policy[4] %></td>
<td><%= policy[5] %></td>

To:

<% policy.each do |p| %>
  <td><%= p %></td>
<% end %>
like image 139
Igor Pantović Avatar answered Sep 21 '22 16:09

Igor Pantović


Do this

  <% @result3.each do |policy| %>
  <tr>
      <% policy.each { |p| raw "<td>#{p}</td>" } %>
  </tr>
  <%end%>
like image 26
Rajdeep Singh Avatar answered Sep 22 '22 16:09

Rajdeep Singh