dennismonsewicz
dennismonsewicz

Reputation: 25542

Rails: Find object with just the ID

I am building an app that uses the PublicActivity gem (https://github.com/pokonski/public_activity), although this gem is a nice way to have a "Facebook"-like activity stream, it also comes at a small price; Database load time.

To help with load time, I am offloading the processing to my Sidekiq processor like so:

ActivityLogWorker.perform_async object_id: @video.id, klass: :video, owner_id: @athlete.id, activity: :create

Here is my worker:

class ActivityLogWorker
  include Sidekiq::Worker
  sidekiq_options queue: :activity_log

  def perform(opts = {})
    obj = "#{opts.delete('klass')}".capitalize.constantize.find(opts.delete('object_id'))
    owner = User.find(opts.delete('owner_id'))
    activity = opts.delete('activity').to_sym
    obj.create_activity activity, owner: owner
  end
end

Is there a better way to pass in the object_id without having to clarify the class that goes along with that object?

Upvotes: 1

Views: 206

Answers (1)

bgates
bgates

Reputation: 2373

I don't know what counts as "better", but you could just pass the object itself and have your worker query it for its id and its class.

Upvotes: 1

Related Questions