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
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
···
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
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.rubyforge.org/
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
···
On 3 Aug 2008, at 22:48, laredotornado wrote:
Any ideas? Thanks, - Dave
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.rubyforge.org/