Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I've <a href="http://www.lonelyplanet.com/members/madlep">done this before</a> I've managed it by having a denormalised <code>UserActivity</code> model or similar with a <code>belongs_to</code> polymorphic association to an <code>ActivitySource</code> - which can be any of the types of content that you want to display (posts, comments, up votes, likes, whatever...).</p> <p>Then when any of the entities to be displayed are created, you have an <code>Observer</code> that fires and creates a row in the <code>UserActivity</code> table with a link to the record.</p> <p>Then to display the list, you just query on <code>UserActivity</code> ordering by <code>created_at</code> descending, and then navigate through the polymorphic <code>activity_source</code> association to get the content data. You'll then need some smarts in your view templates to render comments and posts and whatever else differently though.</p> <p>E.g. something like...</p> <p>user_activity.rb:</p> <pre><code>class UserActivity &lt; ActiveRecord::Base belongs_to :activity_source, :polymorphic =&gt; true # awesomeness continues here... end </code></pre> <p>comment.rb (post/whatever)</p> <pre><code>class Comment &lt; ActiveRecord::Base # comment awesomeness here... end </code></pre> <p>activity_source_observer.rb</p> <pre><code>class ActivitySourceObserver &lt; ActiveRecord::Observer observe :comment, :post def after_create(activity_source) UserActivity.create!( :user =&gt; activity_source.user, :activity_source_id =&gt; activity_source.id, :activity_source_type =&gt; activity_source.class.to_s, :created_at =&gt; activity_source.created_at, :updated_at =&gt; activity_source.updated_at) end def before_destroy(activity_source) UserActivity.destroy_all(:activity_source_id =&gt; activity_source.id) end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload