weird session behavior

Hey guys,

I'm having a hard time with some weird behavior in my application.
Every time I logout a particular has_many relationship seems broken.
When I first login, my relationship user.profile.comments works as
expected, but when I logout and login with another user, I get:

NoMethodError in ProfileController#show
undefined method `comments' for #<Profile:0x46ff26c>

My Models:
class User < ActiveRecord::Base
  has_one :profile

class Profile < ActiveRecord::Base
  has_many :comments
  belongs_to :user

class Comment < ActiveRecord::Base
  belongs_to :profile

And here is my login and logout procedure in my controller:

class UserController < ApplicationController
  def login
      if session[:user] = User.authenticate(params[:user][:login],
        flash[:message] = "Login Successful"
        flash[:warn] = "Login Error"

  def logout
    session[:user] = nil
    flash[:message] = 'Logged out'
    redirect_to :action => 'login'

Any ideas?

Javier Godinez


Well, if I restart the server it works until I logout and login with
the same or another user.
I was wondering if anyone has seen behavior like this before.
I can code around this with something like
Comment.find_all_by_profile_id( but I want rails to
make this relationship transparent.


I am calling it like this:
@profile.comments where @profile = @user.profile
here is the subroutine:

  def show
    if params[:id]
      @user = User.find_by_login(params[:id])
      @user = User.find(

    @profile = @user.profile
    @photos = Photo.find_all_by_profile_id(,
:conditions => ["removed = 0"])
    @photo = Photo.find_by_profile_id(, :conditions =>
["removed = 0 AND main = 1"])
    if @photo.nil?
      @photo = Photo.find(1)

    @spot_pages, @spots = paginate_collection(
      @user.spots.find(:all, :order => 'created_at DESC'),
      :page => params[:page])

    @buddies = User.find_by_sql(
      ["SELECT u.* FROM buddies_users b, buddies_users c, users u " +
        "WHERE b.buddy_id = ? " +
          "AND = b.user_id " +
          "AND b.user_id = c.buddy_id " +
          "AND b.buddy_id = c.user_id", @user])

    @comment_pages, @comments =
      :order => 'created_at DESC'), :page => params[:page])

OK, I think I figured it out, it seems to me that requiring a model
(ActiveRecord::Base) in a controller throws many things off in rails.
Has anyone experienced this before? Things such as what I describe
below don't seem to work right. What I was doing is something like:
require 'user' at the top of some controller.

Thanks for your help...
Javier Godinez

Why are you requring a model in a controller? All models are included automatically.

Well, I've been working on this for a while, learning as I go and I
believe that was something I did erroneously.