【发布时间】:2013-05-24 04:48:48
【问题描述】:
我正在使用 PublicActivity gem 来创建新闻提要。 我已经启动并运行良好,但我希望我的活动与另一个表相关联以显示更多详细信息
目前,当有人发帖时,我的新闻提要只会显示“发布了微博” 我希望新闻提要显示已发布的 MicroPost。 我编辑了 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 中分叉并克隆它
【问题讨论】:
-
您是否尝试过将猴子补丁放入初始化程序中?将其放入 /lib 或 /extras 将不起作用。
-
你能告诉我在里面放什么吗?
标签: ruby-on-rails class methods gem associations