2

I'm trying to use Public_activity gem with parameters.

on my console:

 c = PublicActivity::Activity.find(34)
 PublicActivity::Activity Load (0.2ms)  SELECT "events".* FROM "events" WHERE     
 "events"."id" = ? LIMIT 1  [["id", 34]]
 => #<PublicActivity::Activity id: 34, trackable_id: 17, trackable_type: "Follow",   owner_id: 2, owner_type: "User", key: "follow.destroy", parameters: {:notif=>"on"},    recipient_id: nil, recipient_type: nil, created_at: "2014-08-15 04:44:03", updated_at: "2014-08-15 04:44:03"> 

How do I search in PublicActivity::Activity.all to find the one record with parameters[:notif] = "on" ?

Actually my question is much simpler...how do I use an Active Record Query to find a particular records with a hash! ?

Henri
  • 983
  • 3
  • 11
  • 29
  • The author of the gem has been very helpful. There is no way to search via params since the data is serialized. I actually used another technic – Henri Aug 20 '14 at 00:38

1 Answers1

4

The gem owner suggests adding a custom field. Via: https://github.com/pokonski/public_activity#use-custom-fields-on-activity

"Besides the few fields that every activity has, you can also set custom fields. This could be very beneficial, as parameters are a serialized hash, which cannot be queried easily from the database."

To add a custom field:

Add your custom field to the activities table:

class AddNotifFieldToActivities < ActiveRecord::Migration

  def change
    change_table :activities do |t|
      t.string :notif
    end
  end

end

Assign custom field in the model:

class User < ActiveRecord::Base
  include PublicActivity::Model
  tracked notif: proc {|controller, user| controller.notif }
end

Now you can use a query to find activity records where notif: on

Patrick O'Grady
  • 548
  • 4
  • 13