Skip to main content

How do I add HTTParty to ActiveJob ?

General • Asked by Arnas Klasauskas

I'm currently trying to make a GET request with ActiveJob. It looks like everything is working, but somehow the GET request isn't saving in my database.

This is my TypeformJob:

  def perform(current_user)
    response = HTTParty.get("{current_user.hash_id}",
      headers: { "Authorization" => "Bearer #{@token}"})
    if response.code.to_i == 200
      items = response.parsed_response["items"]
      items.each do |item|
        @order = current_user.orders.find_or_create_by(landing_id: item["landing_id"]) do |order|
          order.landing_id = item["landing_id"]
          order.order_number = item["hidden"]["order_number"]
          order.moduls = item["hidden"]["moduls"]
          order.project = item["hidden"]["project"]
          order.category = item["hidden"]["nickname"]
          order.price = item["hidden"]["price"]
 = item["hidden"]["total"]
          order.summary = item["answers"][0]["text"]
          order.payment = item["answers"][1]["choice"]["label"]
          order.number = item["answers"][2]["number"]
 = item["answers"][3]["date"]
 = item["answers"][4]["choice"]["label"]
      p response

My OrdersController:

  def get_requests

The console responses with this:

[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   Order Load (0.4ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."user_id" = $1 AND "orders"."landing_id" = $2 LIMIT $3  [["user_id", 4], ["landing_id", "16c950ff40b24dc41300a5ceb6cb4e7e"], ["LIMIT", 1]]
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   ↳ app/jobs/typeform_job.rb:12
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]    (0.5ms)  BEGIN
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   ↳ app/jobs/typeform_job.rb:12
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]    (0.2ms)  ROLLBACK
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960]   ↳ app/jobs/typeform_job.rb:12
[ActiveJob] [TypeformJob] [2560dd37-cc4c-4cc3-95e9-5787b65e1960] Performed TypeformJob (Job ID: 2560dd37-cc4c-4cc3-95e9-5787b65e1960) from Async(default) in 1007.91ms

Any thoughts on this? I'm not sure where the ROLLBACK does come from

Hey Arnas!

Anytime you see a ROLLBACK in your logs, that means your validations failed.

In development, you print out what errors there are if it fails for debugging pretty easily.

If this is happening in production, you could change to find_or_create_by! so it throws an exception instead instead of silently failing. Then at least you can debug it through your error tracking.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.