0

我正在使用 PublicActivity gem 创建新闻提要。我已经启动并运行良好,但我希望我的活动与另一个表相关联以显示更多详细信息

目前,当有人发布帖子时,我的新闻提要仅显示“发布了微博”,我希望新闻提要显示已发布的微博。我编辑了 gem 文件以包含一个关联,以便活动在 microposts 表中查找以获取 micropost

module PublicActivity
 module ORM
  module ActiveRecord
   # The ActiveRecord model containing
   # details about recorded activity.
   class Activity < ::ActiveRecord::Base
    include Renderable
    belongs_to :trackable, :polymorphic => true
    belongs_to :owner, :polymorphic => true
    belongs_to :recipient, :polymorphic => true
    serialize :parameters, Hash

    # I added this
    has_one :micropost, foreign_key: "id", primary_key: "trackable_id"
    end
  end
 end
end

这似乎起到了作用,并显示了用户发布的微博,除了我想在不编辑 gem 的情况下这样做。我尝试了许多不同的猴子修补方法,但似乎无法弄清楚。有人可以帮助我吗?

提前致谢

这是我的一些代码

控制器

class ActivitiesController < ApplicationController
 def index
  @activities = PublicActivity::Activity.order("created_at desc").where(owner_id: [current_user.followed_user_ids, current_user], owner_type: "User")
 end
end

看法

<h1>Activities</h1>

<% @activities.each do |activity| if activity.trackable %>
 <div class="activity">
  <%= render_activity activity %>
 </div>
<% end %>
<% end %>

微博活动

<% if activity.trackable %> 
<%= link_to gravatar_for(activity.owner), activity.owner %>
<span class="user">
    <%= link_to activity.owner.name, activity.owner %>
</span>
<span class="content">
    Created a Micropost
    # <%= activity.micropost.content %> COMMENTED OUT WHILE NOT WORKING
</span>
<span class="timestamp">
    <%= time_ago_in_words(activity.created_at) %> ago.
</span>
<% end %>

编辑

此外,当我从作者 GitHub 将 gem 更新为当前的 gem 时,即使编辑 gem 也不起作用。因此,我无法从他的宝石中分叉并克隆它

4

1 回答 1

0
module PublicActivity
 module ORM
  module ActiveRecord
   class Activity < ::ActiveRecord::Base
    has_one :micropost, foreign_key: "id", primary_key: "trackable_id"
   end
  end
 end
end

config/initializers/public_activity_monkey_patch.rb应该这样做。

于 2013-05-29T21:45:26.960 回答