Public_activity gem parameters Rails 3

864 views Asked by At

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! ?

1

There are 1 answers

0
Patrick O'Grady On BEST ANSWER

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