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