如何在公共活动中设置收件人ID

时间:2013-06-16 08:20:30

标签: ruby-on-rails ruby ruby-on-rails-3 gem railscasts

我正在浏览Public Activity Gem Railscast Vid。 (http://railscasts.com/episodes/406-public-activity

我当前的数据库查找/存储除recipient_id之外的所有内容....它显示为nil

#<PublicActivity::Activity id: 108, trackable_id: 133, trackable_type: "Relationship",
owner_id: 1, owner_type: "User", key: "relationship.create", parameters: {}, 
recipient_id: nil, recipient_type: nil, created_at: "2013-06-16 07:37:28", 
updated_at: "2013-06-16 07:37:28">

我希望recipient_id成为正在执行操作的用户....

实施例

如果我喜欢 Tom的帖子 .....我希望Tom的ID成为recipient_id&amp;我将成为owner_id。

我目前设置了一个收件人ID ....使用我的喜欢控制器中的代码....但它只将收件人ID设置为当前用户....这不是我想要的:(

LIKES CONTROLLER

class Like < ActiveRecord::Base
  include PublicActivity::Model
  tracked except: :destroy, owner: ->(controller, model) { controller && controller.current_user }

  **tracked except: :destroy, recipient: ->(controller, model) { controller && controller.current_user }**

  attr_accessible :dailypost_id, :user_id

  belongs_to :user
  belongs_to :dailypost


  default_scope order: 'likes.created_at DESC'
end

我是否需要创建方法或其他内容。请帮助.....新手到轨道

1 个答案:

答案 0 :(得分:11)

tracked recipient: ->(controller, model) { model && model.user }

假设Tom是被跟踪对象的用户。否则,根据您的申请更改model.user