[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why am I getting "undefined method" error?

(D. Alvarado)

8/3/2008 9:45:00 PM

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
4 Answers

Gregory Brown

8/3/2008 9:59:00 PM

0

On Sun, Aug 3, 2008 at 5:48 PM, laredotornado <laredotornado@zipmail.com> wrote:
> 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

Tim Hunter

8/3/2008 9:59:00 PM

0

laredotornado wrote:
> 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

--
RMagick: http://rmagick.ruby...

Frederick Cheung

8/3/2008 10:03:00 PM

0


On 3 Aug 2008, at 22:48, laredotornado wrote:

> 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
> Any ideas? Thanks, - Dave
>


Tim Hunter

8/3/2008 10:58:00 PM

0

laredotornado wrote:

> 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

--
RMagick: http://rmagick.ruby...