我正在尝试将 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] = "on"的一条记录?
实际上我的问题要简单得多...我如何使用事件记录查询来查找带有哈希值的特定记录! ?
最佳答案
gem 所有者建议添加自定义字段。通过: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."
添加自定义字段:
将自定义字段添加到事件表中:
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
的事件记录
关于ruby-on-rails - Public_activity gem 参数 Rails 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25334734/