Public Activity Rails gem 使用一对多关联
Using One-to-Many Associations for the Public Activity Rails gem
我刚开始使用 PublicActivity gem and wanted to display the activities of authors throughout the site. In the app an author has many books. When an author releases a new book I'd like there to be a notification to appear in the feed's of users. Trying my best to translate what's shown in the code 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">
为了保存作者,您最好在创建新书时使用关系
author.books.create(name: "Jaws")
然后如果你想保存 reader,你需要将它添加到 args hash
author.books.create(name: "Jaws", reader: some_user)
注:
这个预填充对象的原因是当你在一个活动记录关系对象上调用new时,条件里面的所有条件都被用来创建新对象,例如
Book.where(author: some_author).new
这将生成一个 book 实例,author_id
将是来自 where 查询的 some_author
的 ID。
所以当我们执行 author.books
这创建了一个查询
Book.where(author_id: author.id)
并且通过调用new,新书将拥有作者的id。
PS:
这也适用于 where
中的多个参数
Model.where(key1: value1, key2: value2, key3: value3).new
将创建一个新实例,其中属性 key1
、key2
、key3
已填充。
我刚开始使用 PublicActivity gem and wanted to display the activities of authors throughout the site. In the app an author has many books. When an author releases a new book I'd like there to be a notification to appear in the feed's of users. Trying my best to translate what's shown in the code 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">
为了保存作者,您最好在创建新书时使用关系
author.books.create(name: "Jaws")
然后如果你想保存 reader,你需要将它添加到 args hash
author.books.create(name: "Jaws", reader: some_user)
注:
这个预填充对象的原因是当你在一个活动记录关系对象上调用new时,条件里面的所有条件都被用来创建新对象,例如
Book.where(author: some_author).new
这将生成一个 book 实例,author_id
将是来自 where 查询的 some_author
的 ID。
所以当我们执行 author.books
这创建了一个查询
Book.where(author_id: author.id)
并且通过调用new,新书将拥有作者的id。
PS:
这也适用于 where
Model.where(key1: value1, key2: value2, key3: value3).new
将创建一个新实例,其中属性 key1
、key2
、key3
已填充。