0

Active Job creates the following SQL on placing a job in its queue.

INSERT INTO "delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["queue", "mailers"], ["handler", "--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActionMailer::DeliveryJob\n  job_id: 189ce295-1da8-47f5-9933-1b5657554731\n  queue_name: mailers\n  arguments:\n  - UserMailer\n  - register_success\n  - deliver_now\n  - _aj_globalid: gid://hub/User/1\n  - _aj_globalid: gid://hub/Order/294\n  - token: 561e6309deb2b9.72817606\n    number: 123\n    expiration: '2025-10-14'\n"], ["run_at", "2015-10-14 14:13:30.140078"], ["created_at", "2015-10-14 14:13:30.140408"], ["updated_at", "2015-10-14 14:13:30.140408"]]

How would I modify this to work as a raw SQL query? I thought that the rails output was supposed to be valid sql, but I get an error

ERROR: syntax error at or near "[" LINE 1: ...at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["queue", ...

2 Answers 2

2

it is using placeholders. so you would need to replace the $x with the values so

INSERT INTO "delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["queue", "mailers"], ["handler", "--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActionMailer::DeliveryJob\n  job_id: 189ce295-1da8-47f5-9933-1b5657554731\n  queue_name: mailers\n  arguments:\n  - UserMailer\n  - register_success\n  - deliver_now\n  - _aj_globalid: gid://hub/User/1\n  - _aj_globalid: gid://hub/Order/294\n  - token: 561e6309deb2b9.72817606\n    number: 123\n    expiration: '2025-10-14'\n"], ["run_at", "2015-10-14 14:13:30.140078"], ["created_at", "2015-10-14 14:13:30.140408"], ["updated_at", "2015-10-14 14:13:30.140408"]]

would be come

INSERT INTO "delayed_jobs" ("queue", "handler", "run_at", "created_at", "updated_at")
 VALUES (
 'mailers',
 '--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper\njob_data:\n  job_class: ActionMailer::DeliveryJob\n  job_id: 189ce295-1da8-47f5-9933-1b5657554731\n  queue_name: mailers\n  arguments:\n  - UserMailer\n  - register_success\n  - deliver_now\n  - _aj_globalid: gid://hub/User/1\n  - _aj_globalid: gid://hub/Order/294\n  - token: 561e6309deb2b9.72817606\n    number: 123\n    expiration: '2025-10-14'\n',
 '2015-10-14 14:13:30.140078', 
 '2015-10-14 14:13:30.140408',
 '2015-10-14 14:13:30.140408'
) RETURNING "id"
Sign up to request clarification or add additional context in comments.

1 Comment

oops, bad job on the cut and paste, fixed thanks @muistooshort
1

If you have access to the ActiveRecord object that generated that database call, you can use the to_sql method to get the SQL query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.