1st Part: The query to the BBDD:
What you want to do is consult your database using ActiveRecord, regardless of where you do it (in this case in a Job). If you need all the IDs of your table you can do:
@credenciales = Credential.all
# Usando ActiveRecord busca todas las entradas de la tabla "credentials" y las
# trae en un hash llamado "active_record_relation". Ese Hash está ahora en tu variable
# @credenciales
From now on you can do whatever you want with that relationship. In the case that you want to create a "job" for each entry of the table you could do this:
@credenciales.each do |credential|
GetApisDataJob.perform_later(credential.id)
end
# "each" itera por cada uno de los elementos del hash y el bloque que le
#pasamos en este caso crea el job.
If you want a single Job to do all the work you can pass a list of IDs using "pluck":
@ids_de credenciales = @credenciales.pluck(:id) # Devuelve un array. Ejemplo [1,2,3]
Then you could send that array to your job:
GetApisDataJob.perform_later (@ids_de_credenciales)
Of course now the work should know what to do with the array:
class SendBeersDeliveredEmailJob < ActiveJob::Base
queue_as :default
def perform(array_de_ids)
credenciales = Credential.find(array_de_ids)
credenciales.each do |credencial|
#Lo que quiero hacer con cada credencial
end
end
end
2nd Part: The Job. There are several things to correct.
-
Name: When you use the rails generator do not include "job" in the title because the generator already does it. To create GetApisDataJob you must use:
rails generate job GetApisData
-
The syntax: To call a job you do not pass the parameters where you are doing it but to the "perform" action, like this:
GetApisDataJob.perform_later(lo_que_pasas.id)
-
Where you execute it: It is possible to call the same job from within the same as you do in your example, but that does not seem to make sense. A loop would be made that would never stop.
-
Parameters that you pass: In the case of jobs try to pass very little data because it will include everything you pass on your BBDD redis. Ideally you pass only IDs and little text.