views:

73

answers:

1

I'm still pretty new to Rails and need your help: I have been creating a social fitness analytics site (zednine.com) that has an activity stream that lists workouts posted on the site. Several pages currently show the 10 most recently updated workouts. I'd like to add a link at the bottom to "Older workouts." On click, this should show the next 10 workouts in the page, immediately below the first 10, with a new link to Older below (now 20 displayed in the page) -- just like the news stream on Facebook and several other social networks.

What I've tried so far:

  1. I'm currently using a find with :limit to get the first N results
  2. I can set up a unique find with :limit and :offset for each set of N results with hidden divs, but that's lame and does not extend well

I also looked at:

  1. pagination, including will_paginate, but not clear whether this can help for in same page chunking?
  2. collections...?

What is the right/a good way to do this?

Also, how can I include records from multiple tables in this sort of stream? E.g., list could include workouts from one table, journal entries from another, comments from a third, all intermixed and sorted by date?

Thank you!

A: 

Will_paginate will do the job, just pass in the page you want:

<%= link_to "Older Posts", model_route_path(:page => next_page) %>

As for the second question, simply create a Feeds model (or tack it onto an existing one). Then have a method which fetches recent entries from the various other models and sorts them by created_at date. I would probably implement #recent method on each of the models and call that in your Feed object.

Models:

class Feed
  def index
    entries = []
    entries << Journal.recent
    entries << Comment.recent
    # etc
    entries.sort_by {|entry| entry['created_at']}
  end
end

class Journal < ActiveRecord::Base
  def recent
    self.find_all_by_created_at(:limit => 10)
  end
end

Or something like that. You will have to be very careful about scalability here.

askegg
Thanks! I'm running into some other problems getting will_paginate to work, but have posted to the will_paginate group. Being a newbie gets oh so tiring... ;)The feed approach makes perfect sense at current scale. Thanks for the pointer!
Olivia