Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Showing Treebook statuses in a "newest first" order

Right now (as far as I've gotten in the Rails app Treebook) the statuses show on the feed page in the order that they're created. ie the oldest status appears at the top. How can I change the order so it's like facebook/twitter/anySocialNetwork where the newest posts show up at the top?

3 Answers

Hello,

This worked for me when I tried it. You will want to change your statuses controller to default showing status by newest first.

Right now the index method in the Statuses controller should read:

 @statuses= Status.all 

You will want to change that to:

@statuses = Status.order("created_at DESC").all

This should sort all statuses by newest first. I am not sure if there is a better way to do this or not, but this is what worked for me!

Hi Brett...that works really well, thanks! Now I'm trying to get it to work on the profile page as well. Do you know how to do that?

Unfortunately, I have not found a way yet to do this. :/

Hopefully someone from the staff will tell us how?

Hey Kevin Lehtiniitty,

I found out how to sort the status for the profile pages. It finally hit me today :p

You should have created another controller for the profile pages. In that controller in the show method you should have something like:

def show
@user = User.find_by_profile_name(params[:id])
if @user
  @statuses = @user.statuses.all
  render action: :show
else
  render file: 'public/404', status: 404, formats: [:html]
end

end

In that method you will need change the code to look almost like how it now looks in the statuses controller. it should look like:

def show
@user = User.find_by_profile_name(params[:id])
if @user
  @statuses = @user.statuses.order("created_at DESC").all
  render action: :show
else
  render file: 'public/404', status: 404, formats: [:html]
end

end

That will resort the statuses to show the newest status on top! Let me know if this doesn't work!

Todd Nestor
Todd Nestor
10,689 Points

You could also do it this way, and I imagine if you added the .reverse onto the user.statuses.all it would work as well

def index
    @statuses = Status.all.reverse
  end