s89_
s89_

Reputation: 1733

Rails ArgumentError on Job - wrong number of arguments - how to debug?

I’m trying to create a Job within my specs with the following, but keep getting an ArgumentError:-

MyJob.new.perform_now(user_id: @current_user.id, building_ids: [building.id])

module ActionPlan
class MyJob < ApplicationJob
queue_as :low

def perform(user_id:, building_ids:, **_args)
  @user = User.find(user_id)
  @buildings = ActionPlan::Reminder.where(user_id: user_id)
  @buildings.map(&method(:create_alerts))
end

I keep getting wrong number of arguments (given 1, expected 0) (ArgumentError). Where am I going wrong? The perform method takes 2 arguments, right? How would I debug this?

Upvotes: 0

Views: 1387

Answers (1)

Mark
Mark

Reputation: 6455

Try this:

module ActionPlan
  class MyJob < ApplicationJob
    queue_as :low

    def initialize(user_id:, building_ids:, **_args)
      @buildings = ActionPlan::Reminder.where(user_id: user_id)
    end

    def perform
      buildings.map(&method(:create_alerts))
    end

    private

    attr_accessor :buildings
  end
end

That should allow you to call:

MyJob.new(user_id: @current_user.id, building_ids: [building.id]).perform

Upvotes: 2

Related Questions