ruby-on-railspublic-activity

Rails 5 ArgumentError (wrong number of arguments (1 for 0)


In my Rails 5 app, I'm using the Public_Activity gem and I'm trying to record an activity, but running into an error: ArgumentError (wrong number of arguments (1 for 0).

I tried to record the activity in my controller, but I was having trouble because I couldn't figure out how to specify my model instance (CanvasProduct). So I tried to create the entry in the model using what I found in the documentation.

my controller (creates a CanvasProduct):

def add_product
  product = Product.friendly.find(params[:product_id])        
  if @canvas.canvas_products.create product: product
    render nothing: true, status: :created
  else
    render nothing: true, status: :bad_request
  end
end

My model

class CanvasProduct < ActiveRecord::Base
  belongs_to :canvas
  belongs_to :product

  include PublicActivity::Common

  after_create :create_activity

  def create_activity
    @cp = self
    @cp.create_activity :create, recipient: @cp.canvas
  end  
end

Solution

  • I think the problem is with your after_create callback method name. You have defined it with same name as of the gem's method name. Hence it just overrides the gem's inbuilt method. So when you call:

    @cp.create_activity :create, recipient: @cp.canvas
    

    You are actually calling the instance method create_activity defined in your model instead of the method provided by the gem. And notice that, your defined method accepts no arguments but in the above line you are actually passing arguments.

    So can you just try renaming the after_create callback method name to something else ? Like:

    after_create :create_new_activity
    
    def create_new_activity
        @cp = self
        @cp.create_activity :create, recipient: @cp.canvas
    end