Issue displaying user posts

Hello there,

On my site I have a list of users, each of which can post status updates on their profile. Each user can search for other users and view their profiles too.

The odd thing is, if I log in and out of each user and look at their status, their posts are specific to them (which is what i want) However...... If I search for a user and click to view their profile, it seems to display the posts of the logged in user which is what i dont want.

Below is the show method which is what the profile page uses. I have tried limiting it to 1 post for a user, and also tried to set the initial param before the find to try and get around the issue. Unfortunately these didn't work..

  def show     username = params[:username]     @user = User.find_by_username(username)   if @user       @title = "Profile page of: #{username}"       @info = @user.info ||= Info.new       @posts = Post.find_by_user_id( session[:user_id], :order => "created_at DESC")         respond_to do |format|         format.html       end   else       flash[:notice] = "No user #{username} found!"       redirect_to :action => "index"     end   end Can anyone see why a: It displays the users status updates correctly when i'm logged in as each user and b: why the posts seem to stay as per the logged in user when i click on another user's profile??

Any suggestions, or ways round this appreciated.

Thanks for reading.

** RESOLVED **

If anyone else experiences something like this,

Original line of code: @posts = Post.find_by_user_id( session [:user_id], :order => "created_at DESC")

change it to:

@posts = Post.find_by_user_id( @user.id, :limit => 1, :order => "created_at desc")

Note that the preferred way to do this is from the other side of the association:

@user.posts.find(:first, :order => 'created_at DESC')

or even to define a 'recent' scope on Post:

named_scope :recent, :order => 'created_at DESC'

and then the line in the controller reduces to:

@post = @user.posts.recent.first

--Matt Jones