添加或编辑gem而不编辑gem

时间:2013-05-29 03:51:35

标签: ruby-on-rails class methods gem associations

我正在使用PublicActivity gem来创建新闻Feed。 我已经把它运行得很好,但我希望我的活动与另一个表关联以显示更多细节

目前我的新闻Feed只是在有人发帖时说“发布了一个微博” 我希望新闻Feed显示已发布的MicroPost。 我编辑了gem文件以包含一个关联,以便活动在microposts表中查找以获得微博

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也无法正常工作。因此,我还没有能够从他的宝石中分叉并克隆它

1 个答案:

答案 0 :(得分: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中的

应该这样做。