Take the 2-minute tour ×

I'm stuck on the last exercise in the Hartlt book 12.3.4 where you define the status feed for yourself and the users you are following. I'm a Rails newbie so just let me know if I need to post additional information.

When I tail the development log I can see will_paginate fire the SQL to gather the initial records, and the initial page looks fine when it is served up. When I click a link to go to any another page, it appears will_paginate doesn't fire the SQL to get retrieve more data from the database as the next page is server up fine, but there is no data.

There are also no new entries in the development log and maybe I'm wrong, but I think this indicates will_paginate didn't hit the database.

I tried to include all the relevant code snippits below. Happy to send anything that's missing.

Here is my pages_controller.rb

def home
  @title = "Home"
  if signed_in?
    @micropost = Micropost.new
    @feed_items = current_user.feed.paginate(:page => params[:page])

Here is my user.rb

def feed

Here is my microposts.rb

scope :from_users_followed_by, lambda { |user| followed_by(user) }


def self.followed_by(user)
  following_ids = %(SELECT followed_id FROM relationships
                    WHERE follower_id = :user_id)
  where("user_id IN (#{following_ids}) OR user_id = :user_id",
        :user_id => user)

NOTE: In the video tutorial (chapter 12, time = 02:06:00) and the book (page 517, listing 12.44) Harlt uses the variable "followed_ids" instead of "following_ids". In the virgin sample code you can download from his site the variable name is "following_ids", and I have tried it both ways - but it fails.

Bottom line - the dev log shows will_paginate retrieving the first batch of data, but it never goes back to the database for additional data.

Can anyone suggest what I can take a look at to resolve my problem?

Many thanks.

share|improve this question

1 Answer 1

do you have more records than will fit on one page?

If not, it may be that will_paginate is doing a count first. Then deciding not to load any records.

Get into the console and try it out.

$ rails console

user = User.find(23)
# the same user you're logged in as

# is it more than one page?

user.feed.paginate(:page => 1)
# 20 records?

user.feed.paginate(:page => 2)
# nothing?


in your example scope :from_users_followed_by, lambda { |user| followed_by(user) } is redundant.

You may as well just say;

def feed
share|improve this answer

Your Answer


