NoMethodError even though the method is there

Hi,

I’m sure this will be an easy one, I’m just not seeing something
really obvious. In my app/controllers/sessions_controller.rb file, I
have this method:

def create
if (log_user_in(params[:login], params[:password],
params[:remember_me]))
user = User.find(session[:user_id])
if (user.is_subscriber?) # line
11
flash[:notice] = “Logged in successfully”
redirect_to :controller => ‘admin’
else
flash[:notice] = “Logged in successfully”
redirect_to :controller => ‘order’, :action => ‘new’
end
else
flash[:notice] = “Authentication failed.”
render :controller => ‘register’, :action => ‘start’
end
end

and in my User model, I have this method:

class User < ActiveRecord::Base

def self.is_subscriber?
user_type_id == 1
end

end

but I’m getting this error:

NoMethodError in SessionsController#create
undefined method is_subscriber?' for #<User:0xb795d668> RAILS_ROOT: /usr/local/apache2/htdocs/easyrx Application Trace | Framework Trace | Full Trace /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:200:inmethod_missing’
app/controllers/sessions_controller.rb:11:in `create’

What obvious thing am I missing?

Thanks, - Dave

Hey Dave, I think your problem is that you have “def
self.is_subscriber?” instead of “def is_subscriber?”, which makes it a
class method instead of an instance method. Try taking out the “self.”
part.

Liam

[email protected] wrote:

Hi,

I’m sure this will be an easy one, I’m just not seeing something
really obvious.

in my User model, I have this method:

class User < ActiveRecord::Base

def self.is_subscriber?
user_type_id == 1
end

end

but I’m getting this error:

NoMethodError in SessionsController#create
undefined method is_subscriber?' for #<User:0xb795d668> RAILS_ROOT: /usr/local/apache2/htdocs/easyrx Application Trace | Framework Trace | Full Trace /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ attribute_methods.rb:200:inmethod_missing’
app/controllers/sessions_controller.rb:11:in `create’

What obvious thing am I missing?

Thanks, - Dave

def … end in not with in the clas Xyz end…

On Feb 6, 2008 12:53 AM, Liam M. [email protected]
wrote:

Hi,
end
attribute_methods.rb:200:in `method_missing’


Thanks & Regards,
Pavan A.

NOTICE : This transmission contains information that may be confidential
and
that may also be privileged. Unless you are the intended recipient of
the
message or authorized to receive it for the intended recipient, you may
not
copy, forward, or otherwise use it, or disclose it or its contents to
anyone
else. If you have received this transmission in error please notify us
immediately and delete it from your system.