render all records in a stored procedure - ruby-on-rails

I am trying to render all the data of a stored procedure in a table in my view but the problem is that the name of the columns can change in each stored query so I can not make the specific field call in the render because each query brings different columns, how could I make the call of the columns name for the (table titles) and render all the data in the unspecified in the name of the data but bring all the results of the query that apparently returns them in an array?
this is my stored_procedures_controller.rb
class StoredProceduresController < ApplicationController
before_action :authenticate_usuario!
def cantidad_productosxpedido
#cantidad_productosxpedido = Stored_procedure.fetch_db_records("exec SPAD_ReporteProductosxPedido '#{Time.now.try(:strftime,"%Y%m%d")}','#{params[:search]}','#{params[:search6]}'")
respond_to do |format|
format.html
format.js
format.xls
end
end
end
this is my model stored_procedure.rb
class Stored_procedure < ActiveRecord::Base
def self.fetch_db_records(proc_name_with_parameters)
connection.select_all(proc_name_with_parameters)
end
def self.insert_update_delete_calculate(proc_name_with_parameters)
connection.execute(proc_name_with_parameters)
end
def self.fetch_val_from_sp(proc_name_with_parameters)
connection.select_value(proc_name_with_parameters)
end
end
I usually do the rendering of the stored this way
<table class="table table-striped table-bordered table-condensed table-hover rwd_auto">
<thead>
<tr>
<th>Ruta</th>
<th>Pedido</th>
<th>IdCliente</th>
<th>Cliente</th>
<th>Nombre Comercial</th>
<th>2010100</th>
<th>2020100</th>
<th>2020200</th>
<th>2020500</th>
<th>2021200</th>
<th>2200500</th>
<th>2200501</th>
<th>2200508</th>
<th>2200509</th>
<th>2201000</th>
<th>2203100</th>
<th>2203101</th>
<th>2203102</th>
<th>2203104</th>
<th>2203108</th>
<th>2203200</th>
</tr>
</thead>
<tbody id="container_productosxpedido">
<%= render partial: "/stored_procedures/cantidad_productoxpedido", collection: #cantidad_productosxpedido %>
</tbody>
</table>
and in the partial _cantidad_productoxpedido
<tr>
<td><%= cantidad_productoxpedido['Ruta'] %></td>
<td><%= cantidad_productoxpedido['Pedido'] %></td>
<td><%= cantidad_productoxpedido['IdCliente'] %></td>
<td><%= cantidad_productoxpedido['Cliente'] %></td>
<td><%= cantidad_productoxpedido['NombreCorto'] %></td>
<td><%= cantidad_productoxpedido['2010100'] %></td>
<td><%= cantidad_productoxpedido['2020100'] %></td>
<td><%= cantidad_productoxpedido['2020200'] %></td>
<td><%= cantidad_productoxpedido['2020500'] %></td>
<td><%= cantidad_productoxpedido['2021200'] %></td>
<td><%= cantidad_productoxpedido['2200500'] %></td>
<td><%= cantidad_productoxpedido['2200501'] %></td>
<td><%= cantidad_productoxpedido['2200508'] %></td>
<td><%= cantidad_productoxpedido['2200509'] %></td>
<td><%= cantidad_productoxpedido['2201000'] %></td>
<td><%= cantidad_productoxpedido['2203100'] %></td>
<td><%= cantidad_productoxpedido['2203101'] %></td>
<td><%= cantidad_productoxpedido['2203102'] %></td>
<td><%= cantidad_productoxpedido['2203104'] %></td>
<td><%= cantidad_productoxpedido['2203108'] %></td>
<td><%= cantidad_productoxpedido['2203200'] %></td>
</tr>
but when changing the name of the columns I do not know how to do to display the data without specifying the names of those columns
UPDATE
I could resolve to display the contents of the table as follows
<%#cantidad_productosxpedido.to_a.each do |foo|%>
<tr>
<%foo.each do |label, value|%>
<td><%= value %></td>
<%end%>
</tr>
<%end%>
but now I try to add the headers of the table
like this:
<%#cantidad_productosxpedido.first.each do |foo|%>
<tr>
<%foo.each do |label, value|%>
<th><%= label %></th>
<%end%>
</tr>
<%end%>
but it does not show correct in the table but renders it as follows
<tr>
<th>title1</th> <td>value1</th>
</tr>
<tr>
<th>title2</th> <th>value2</th>
</tr>

object.attributes.keys will return an array of column names for a given record. You can use that to generate your columns as follows:
<% #cantidad_productosxpedido.each do |cantidad_productoxpedido| %>
<% cantidad_productoxpedido.keys.each do |col| %>
<td><%= cantidad_productoxpedido[col] %></td>
<% end %>
<% end %>
Not tested, but that should work.

Related

Sort Date (Newest to Oldest) ROR

this is index.html.erb
<%= link_to_modal_new(new_master_film_path, "Enter film") %>
<%= link_to_export("Master films", params) %>
<%= paginate #master_films %>
<table class="table table-condensed table-hover">
<thead>
<tr>
<th></th>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= render #master_films %>
</tbody>
</table>
I need to organize the Date column in desc (newest to oldest) dates. I added the following dropdown option but it fails. Error template. Or if I can default the sort to newest Laminated date is first.
<%= render 'shared/sort_dropdown', current: sort, choices: [['serial','desc'], ['laminated','desc']] %>
I am writing this in detail, assuming you are very new to rails because of the way you asked the question and the way your code of view is. My apologies, if I am wrong.
In your controller (I guess it should be films_controller.rb or master_films_controller.rb), the action in which you are calculating this #master_films (it will have same name as the view that has the code you posted in question), add this:
#master_films = MasterFilm.all.order('laminated DESC')
# OR
#master_films = MasterFilm.all.order(laminated: 'desc')
# letter case does not matter
Your table code in view is not correct. It should be:
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Formula</th>
<th>Mix/g</th>
<th>Mach</th>
<th>ITO top</th>
<th>Thinky</th>
<th>Chemist</th>
<th>Operator</th>
<th>Inspector</th>
<th>Eff W</th>
<th>Eff L</th>
<th>Yield</th>
<th>Defects</th>
<th>Laminated</th>
<th>Note</th>
</tr>
</thead>
<tbody>
<%= #master_films.each do |master_film| %>
<tr>
<td><%= master_film.serial %></td>
<td><%= master_film.formula %></td>
<td><%= master_film.mix %></td>
<td><%= master_film.mach %></td>
<td><%= master_film.ito_top %></td>
<td><%= master_film.thinky %></td>
<td><%= master_film.chemist %></td>
<td><%= master_film.operator %></td>
<td><%= master_film.inspector %></td>
<td><%= master_film.eff_w %></td>
<td><%= master_film.eff_l %></td>
<td><%= master_film.yield %></td>
<td><%= master_film.defects %></td>
<td><%= master_film.laminated %></td>
<td><%= master_film.note %></td>
<tr>
</tbody>
</table>

how to write each loop for has_many in rails?

employee has many skills
skill belongs_to employee
Skills table have employee_id(foreign key).
I want to display in view
There are 4 ways to get the collections of users.
#employees = Employee.all.joins(:skills)
#employees = Employee.all.includes(:skills)
#employees = Employee.all.eager_load(:skills)
In your view add below code in HTML/HAML/SLIM file.
<% #employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.map(&:title).join(", ") %></td>
</tr>
<% end %>`
You could try with something like this.
In controller return all the employees
so it will be like
#employees = Employee.all.includes(:skills)
and in the view you can do
<% #employees.each do |employee| %>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.collect(&:title).join(", ") %></td>
</tr>
<% end %>
Best practice in this case is to write an instance method on your Employee class:
class Employee < ApplicationRecord
has_many :skills
def skills_list
skills.pluck(:title).join(', ')
end
end
And use it like this in your view:
<table>
<tr>
<td><%= #employee.id %></td>
<td><%= #employee.name %></td>
<td><%= #employee.skills_list %></td>
<tr>
</table>
You can use:
<table>
<tr>
<th>Employee </th>
<th>Skills</th>
</tr>
<% #employees.each do |employee|%>
<tr>
<td><%= employee.name %></td>
<td><%= employee.skills.map(&:name).join(', ') %></td>
</tr>
<% end %>
</table>
This above code will list all skill names of each employee.
You can read a complete guide here

How can I fix Blank arrays on Ruby on Rails views?

I am trying to display some information in my views, but it doesn't return any information and it appears completely blank in the view
that's my controller:
class Spree::Admin::StocklogController < Spree::Admin::BaseController
before_action :load_movements
def index
end
def load_movements
#stockmovelog = Spree::StockMovement.all
end
end
And that's my view:
<% #stockmovelog.each do |movement| %>
<tr>
<td><% movement.id %></td>
<td class="align-center"><% movement.quantity %></td>
<td><% movement.created_at %></td>
<td class="align-right"><% movement.updated_at %></td>
<td class="actions"><% movement.action %></td>
</tr>
<% end %>
It will simply create the number of rows from each and keep the content blank, there is something I might be doing wrong?
Just add = to the ERB inside the td's:
<% #stockmovelog.each do |movement| %>
<tr>
<td><%= movement.id %></td>
<td class="align-center"><%= movement.quantity %></td>
<td><%= movement.created_at %></td>
<td class="align-right"><%= movement.updated_at %></td>
<td class="actions"><%= movement.action %></td>
</tr>
<% end %>

Loop multiple variable to form one table

What would be the best method to loop multiple variables into one table? Each variable could have one or more data points. For example, I wish to create something like this:
The following code example is only placing each item in a row.
<table class="table">
<% (#variable1 + #variable2 + #variable3).each do |data, v2, v3| %>
<tr>
<% if data.is_a?(Variable1) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% elsif data.is_a?(Variable2) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% elsif data.is_a?(Variable3) %>
<td><%= data.date %></td>
<td><%= data.name %></td>
<% end %>
</tr>
<% end %>
</table>
In this case, since you don't know how many records might be in each array, I would find out the greatest length of the arrays I have.
In your controller for this aciton could do something like:
#max_length = [#variable_1, #variable_2, #variable_3].map(&:size).max
*Note: this assumes those variables are always arrays, even if they only have a length of 1.
Then in your view file you could do this:
<table class="table">
<% #max_length.times do |n| %>
<tr>
<td><%= #variable_1[n].try(:date) %></td>
<td><%= #variable_1[n].try(:name) %></td>
<td><%= #variable_2[n].try(:date) %></td>
<td><%= #variable_2[n].try(:name) %></td>
<td><%= #variable_3[n].try(:date) %></td>
<td><%= #variable_3[n].try(:name) %></td>
</tr>
<% end %>
</table>
This way, for the third row, for example, in columns 1 and 3 in your example there will be no values, but values for the second column, which has a third record, will appear.

Rails 5 controller dumping data from database as an array

I'm just starting out in learning Ruby on Rails. I have the following code in movies_controller.
class MoviesController < ApplicationController
def index
#movies = Movie.all
end
end
and my index.html.erb has a table structure to display the data from the database as follows:
<h4><%= pluralize(#movies.size, 'Movie') %> Found</h4>
<table>
<tr>
<th>Movie Title</th>
<th>Rating</th>
<th>Gross Revenue</th>
</tr>
<%= #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<td><%= number_to_currency(movie.total_gross) %></td>
</tr>
<% end %>
</table>
The index.html.erb first dumped the whole movies data as an array and then display the data below it in the table structure shown above. Please what have I got wrong? Thanks.
Remove the = sign from the each block
<% #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<td><%= number_to_currency(movie.total_gross) %></td>
</tr>
<% end %>
You have a extra = when you start the loop. That page should look like
<h4><%= pluralize(#movies.size, 'Movie') %> Found</h4>
<table>
<tr>
<th>Movie Title</th>
<th>Rating</th>
<th>Gross Revenue</th>
</tr>
<% #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<td><%= number_to_currency(movie.total_gross) %></td>
</tr>
<% end %>
</table>

Resources