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
end

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

class Comment < ActiveRecord::Base
belongs_to :profile
end

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

class UserController < ApplicationController
def login
if request.post?
if session[:user] = User.authenticate(params[:user][:login],
params[:user][:password])
flash[:message] = “Login Successful”
redirect_to_stored
else
flash[:warn] = “Login Error”
end
end
end

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

Any ideas?

Thanks,
Javier G.

Have you tried restarting your server? By the looks of things, it’s not
getting that has_many definition in Profile.

Ryan,

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(@user.profile.id) but I want rails to
make this relationship transparent.

Thanks,
Javier

How are you calling comments at the moment?

On Dec 28, 2007 2:48 PM, Javier G. [email protected] wrote:

Thanks,


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

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 G.

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])
else
@user = User.find(current_user.id)
end

@profile = @user.profile
@photos = Photo.find_all_by_profile_id(@user.profile.id,

:conditions => [“removed = 0”])
@photo = Photo.find_by_profile_id(@user.profile.id, :conditions =>
[“removed = 0 AND main = 1”])
if @photo.nil?
@photo = Photo.find(1)
end

@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 u.id = b.user_id " +
      "AND b.user_id = c.buddy_id " +
      "AND b.buddy_id = c.user_id", @user])

@comment_pages, @comments =

paginate_collection(@profile.comments.find(:all,
:order => ‘created_at DESC’), :page => params[:page])
end

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

Thanks.

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

On Jan 3, 2008 3:37 PM, Javier G. [email protected] wrote:

On Dec 27, 2007 8:25 PM, Javier G. [email protected] wrote:

end
@spot_pages, @spots = paginate_collection(
@comment_pages, @comments =

Comment.find_all_by_profile_id(@user.profile.id) but I want rails to


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.