0
我刚开始使用PublicActivity宝石,并希望在整个网站上显示作者的活动。在应用程序作者有很多书籍。当作者发布新书时,我希望有一条通知出现在供稿的用户中。尽我所能来翻译一下代码example与此处显示的是我想出迄今:使用公共活动导轨的一对多关联宝石
模式
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
和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">
所以收件人就像'current_user'? – 2015-04-01 19:31:24
反正该行应该是'author.books.create(name:'Jaws')',以便新书将包含作者的ID – 2015-04-01 19:32:34
是的收件人将是'current_user'。当我运行该命令时,尽管所有者ID仍然为零。 – 2015-04-01 19:59:51