Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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