Public_activity gem参数Rails 3

时间:2014-08-15 21:52:19

标签: ruby-on-rails hash gem public-activity

我试图将Public_activity gem与参数一起使用。

在我的控制台上:

 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"> 

如何在PublicActivity :: Activity.all中搜索带有参数的一条记录[:notif] =&#34; on&#34; ?

实际上我的问题要简单得多......如何使用Active Record Query查找带有哈希的特定记录! ?

1 个答案:

答案 0 :(得分:3)

宝石所有者建议添加自定义字段。途经:https://github.com/pokonski/public_activity#use-custom-fields-on-activity

  

&#34;除了每个活动都有的几个字段外,您还可以设置自定义字段。这可能非常有用,因为参数是序列化哈希,无法从数据库中轻松查询。&#34;

添加自定义字段:

将自定义字段添加到活动表:

class AddNotifFieldToActivities < ActiveRecord::Migration

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

end

在模型中指定自定义字段:

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

现在,您可以使用查询查找notif: on

所在的活动记录