public-activity / public_activity

Easy activity tracking for models - similar to Github's Public Activity

Home Page:https://github.com/pokonski/public_activity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create activities manually?

peyloride opened this issue · comments

Hello,

First of all, thanks for the gem. I really appreciate your work. About my question, I'm designing some kind of announcement system and I want to create multiple activities. For example I tried to do following;

PublicActivity::Activity.create!(
  trackable_id: 13, 
  trackable_type: 'Announcement', 
  key: 'shared.announcement', 
  recipient_id: 136130, 
  recipient_type: 'User'
)

But trackable and recipient fields set to nil (because of some callbacks?). As far as I understand, I need to do this;

PublicActivity::Activity.create!(
  trackable: Announcement.find(13), 
  key: 'shared.announcement', 
  recipient: User.find(136130)
)

But this means 2 extra SQL request per activity. How can I solve this? I tried to look for documentation but didn't find an answer.

Thanks!

I haven't looked into why this happens, but you can always do this as a hack:

PublicActivity::Activity.create!(
  trackable: Announcement.new(id: 13), 
  key: 'shared.announcement', 
  recipient: User.new(id: 136130)
)

Thanks! As an another workaround, I can make it via PublicActivity::Activity.new and save it.

@peyloride what kind of Rails version do you have? Activity has no callbacks so it's mostly likely Rails related

See: https://github.com/chaps-io/public_activity/blob/1-5-stable/lib/public_activity/orm/active_record/activity.rb

@pokonski Rails version '4.1.13', I do not have any callback about Public Activity, weird.

@peyloride did you solve thie issue?

I will close it as solved, if it still happens we can reopen :)