Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.00 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I make a newsfeed without many join statements
  2. @user = current_user # (?)
  3. recent_since = 24.hours.ago
  4. @news_feed = []
  5.  
  6. # 1) I want my newsfeed to include when public projects are created.
  7. @news_feed += Project.recent.open
  8. # 2) When you are invited to a private project.
  9. @news_feed += @user.invites.received.pending
  10. # 3) When a user follows a project.
  11. @news_feed += @user.user_following_relationships.recent
  12. # 4) And then all of the actions of the other users that you're following.
  13. @news_feed += @user.follows.collect(&:activities)
  14. # 5) Then for the private projects I have another join table to determine who has access to the projects. (There are also comments on each of these projects that I want to show up in the newsfeed as well).
  15. @news_feed += @user.projects.closed
  16.  
  17. @news_feed.sort!{ |a,b| a.created_at <=> b.created_at }
  18.        
  19. scope :recent, :conditions => ["created_at >= ?", 24.hours.ago]
  20. scope :open, :conditions => "publicity = 'Public'"
  21. scope :closed, :conditions => "publicity = 'Private'"