Why am I getting "undefined method" error?

L

laredotornado

Hi,

I have a model, in which I have defined a method ...

class Form < ActiveRecord::Base
has_many :form_items
validates_associated :form_items

....

def self.has_items
FormItem.count:)all, :conditions => ["form_id
= ?", :id])
end
end


but when i try and call this method from another class,

def new
@form = Form.find_by_user_id(session[:user_id])
if (@form == nil)
false
else
@form.has_items
end
end


I'm getting the error below:

undefined method `has_items' for #<Form:0xb78f5e3c>

Any ideas? Thanks, - Dave
 
G

Gregory Brown

Hi,

I have a model, in which I have defined a method ...

class Form < ActiveRecord::Base
has_many :form_items
validates_associated :form_items

...

def self.has_items
FormItem.count:)all, :conditions => ["form_id
= ?", :id])
end
end

You defined a class method, then used it on an instance. You want an
instance method, so remove self. from in front of the method
definition.

Also, you are passing a symbol instead of the actual id, so you'll
need to fix that too:

def has_items
FormItem.count:)all, :conditions => ["form_id = ?", id])
end

-greg
 
T

Tim Hunter

laredotornado said:
Hi,

I have a model, in which I have defined a method ...

class Form < ActiveRecord::Base
has_many :form_items
validates_associated :form_items

...

def self.has_items
FormItem.count:)all, :conditions => ["form_id
= ?", :id])
end
end


but when i try and call this method from another class,

def new
@form = Form.find_by_user_id(session[:user_id])
if (@form == nil)
false
else
@form.has_items
end
end


I'm getting the error below:

undefined method `has_items' for #<Form:0xb78f5e3c>

Any ideas? Thanks, - Dave

By adding "self." to the has_items method definition, you've defined it
as class method, but @form is an instance of Form. If you want to call
has_items on an instance, remove the "self." in the method definition:

def has_items
...
end
 
F

Frederick Cheung

Hi,

def self.has_items
FormItem.count:)all, :conditions => ["form_id
= ?", :id])
end
end


but when i try and call this method from another class,

def new
@form = Form.find_by_user_id(session[:user_id])
if (@form == nil)
false
else
@form.has_items
end
end
You've defined a class method but you're calling it on an instance
(and it looks like it should by a instance method. You could also do
away with it altogether and right

if @form
@form.form_items.any?
else
false
end

or even

@form && @form.form_items.any?

Fred
 
T

Tim Hunter

laredotornado said:
but when i try and call this method from another class,

def new

In Ruby, defining your own "new" method is rarely done. In Ruby, new is
a class method that allocates a new instance of the class and then calls
the "initialize" instance method to initialize the object. So, if you're
wanting to define the instance method that gets called when you create a
new instance of the class, define the initialize method:

class MyClass
def initialize
# initialization code
end

# other stuff in the class

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top