Take data from a table with activejobs

0

I have a table called credential , which has several attributes but I'm only interested in taking the id attribute, but I want to do it with an activejob . In the activejob they told me that I could get the data with:

class GetapisdatajobsJob < ApplicationJob
  queue_as :default

  def perform(api)
    Credential.all
    each
    {
        GetapisdatajobsJob(Credential).perform_later 
    }
  end
end

It should be noted that I'm just learning ruby and I do not know much, even the syntax costs me and I do not understand it at all, hopefully someone can tell me what I'm doing wrong and what I should do, since all the tutorials I find do not they say nothing, it stays the same. Attentive to your answers.

    
asked by jorge manterola 27.06.2017 в 22:49
source

2 answers

0

To get the id of each object Credential , you can do so 1 :

Credential.all.each do |credential|
  GetapisdatajobsJob(credential.id).perform_later 
end
  • Credential.all generates a collection (object ActiveRecord::Relation ) with all the records found in the model Credential ).
  • each iterates over all the objects, each one passing to the variable credential .
  • credential.id gets the value of id of the object.

1 I assume that in GetapisdatajobsJob(Credential) is where you want to assign the id and that you have a model Credential generated with ActiveRecord .

    
answered by 28.06.2017 в 03:51
0

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.

answered by 08.07.2017 в 18:52