我刚开始使用PublicActivity gem,想在整个站点中显示作者的活动。在应用程序中,作者有很多书。当作者发布新书时,我希望在用户的提要中出现通知。尽我最大的努力翻译代码示例中显示的内容,这是我到目前为止的想法:
楷模
class Reader < ActiveRecord::Base
end
class Author < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
include PublicActivity::Model
tracked owner: :author, recipient: :reader
belongs_to :author, :class_name => "Author"
belongs_to :reader, :class_name => "User"
end
活动控制器
class ActivitiesController < ApplicationController
def index
@activities = PublicActivity::Activity.all
end
end
活动索引视图
<% @activities.each do |activity| %>
<%= activity.inspect %> # Using inspect for now to debug
<% end %>
现在在控制台中,我正在创建书籍并将其附加到作者(author
作为实例变量),如下所示:
author.books << Book.create(name: "Jaws")
活动正在被记录,但实际上所有者应该是作者而接收者应该是用户时,owner_id
and是 nil。recipient_id
#<PublicActivity::Activity id: 1, trackable_id: 1, trackable_type: "Book", owner_id: nil, owner_type: nil, key: "book.create", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2015-04-01 17:36:18", updated_at: "2015-04-01 17:36:18">