I have a list in the model that basically must decide if a record fulfills a function add 1 to good otherwise add 1 to bad and have those two parameters return them to the controller as a hash.
What I do not know is how to get the controller to receive the result.
On the controller
class GruposController < ApplicationController
@grupos = Grupo.all
Contacto.decidir(contador)
Puts contador.inspect #=> me trae nil, cuando quiero que me traiga el total de buenos y malos
…
end
In the model
class Contacto < ApplicationRecord
def self.decidir(contador)
contador = Hash.new
contador[:buenos] = 0
contador[:malos] = 0
@grupos.each do |grupo|
if grupo.nombre
contador[:buenos] += 1
else
contador[:malos] += 1
end
end
UPDATED
Driver:
def index
@grupos = Grupo.all
Contacto.decidir(@grupos)
puts '................'
puts params[:contador].inspect #=> me trae nil, cuando quiero que me traiga el total de buenos y malos
puts '................'
end
model:
def self.decidir(grupos)
puts 'dentro'
{ buenos: 0, malos: 0 }.tap do |contador|
grupos.each do |grupo|
contador[grupo.nombre ? :buenos : :malos] += 1
puts grupo.nombre
end
end
end
log output:
Started GET "/" for 127.0.0.1 at 2017-09-20 10:36:33 -0400
Processing by EntradaController#indice as HTML
Rendering entrada/indice.html.erb within layouts/application
Rendered entrada/indice.html.erb within layouts/application (4.0ms)
Completed 200 OK in 526ms (Views: 484.8ms | ActiveRecord: 0.0ms)
Started GET "/grupos" for 127.0.0.1 at 2017-09-20 10:36:36 -0400
Processing by GruposController#index as HTML
dentro
Grupo Load (1.0ms) SELECT "grupos".* FROM "grupos"
casa
trabajo
deporte
playa
................
nil
................
Rendering grupos/index.html.erb within layouts/application
Rendered grupos/index.html.erb within layouts/application (4.0ms)
Completed 200 OK in 450ms (Views: 410.8ms | ActiveRecord: 1.0ms)
app on github: link