# Model problem

**URL:** <https://www.ruby-forum.com/t/model-problem/132939>\
**Category:** Rails\
**Created:** [March 25, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/model-problem/132939 "2008-03-25T23:08:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 25, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/model-problem/132939/1 "2008-03-25T23:08:54Z")

</div>

because I am using Filemaker pro 9 and the Rfm extension I cannot use  
ActiveRecord. But of course I would like to use the models to have my  
database connection, selection things in.

But I can’t manage to let this work.  
This is one of the many things I tried:

the controller:

class AnimalsController \< ApplicationController  
def index  
@id= Animal.item  
end  
end

the model:

class Animal  
def item  
return 2  
end  
end

I hope someone can see the (probably stupid) fault here 😛

thanxs allot !  
gr heldopslippers

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 12:18am UTC](https://www.ruby-forum.com/t/model-problem/132939/2 "2008-03-26T00:18:31Z")

</div>

Frederick C. wrote:

> Le 25 Mar 2008 ï¿½ 22:08, Heldop S. a ï¿½crit :
> 
> > class AnimalsController \< ApplicationController  
> > def index  
> > @id= Animal.item  
> > end  
> > end
> 
> You’ve created an instance method called item in your model, but  
> you’re using it as a class method. I can’t guess what you’re trying to  
> do, but either you change your controller so that it’s working with an  
> instance of Animal, or make the item method a class method (def  
> self.item …)
> 
> Fred

thanxs for the reply ! 😃  
and well what i want is to have al my database methods in the model (of  
course 😉 ).  
But beceause I use Rfm I can not use ActiveRecord.

this is a sample code that should (I think work). but doens’t …

the controller:

class AnimalsController \< ApplicationController  
def index  
@id = Animals.item  
end  
end

model:

class Animals  
def item  
return 2  
end  
end

The Animals should be a class and the item should be a method.  
but when I try to run this it gives: undefined method `item’ for  
Animals:Class

why ?

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 25, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/model-problem/132939/3 "2008-03-25T23:36:43Z")

</div>

Le 25 Mar 2008 à 22:08, Heldop S. a écrit :

> class AnimalsController \< ApplicationController  
> def index  
> @id= Animal.item  
> end  
> end

You’ve created an instance method called item in your model, but  
you’re using it as a class method. I can’t guess what you’re trying to  
do, but either you change your controller so that it’s working with an  
instance of Animal, or make the item method a class method (def  
self.item …)

Fred

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 9:15am UTC](https://www.ruby-forum.com/t/model-problem/132939/4 "2008-03-26T09:15:44Z")

</div>

Heldop S. wrote:

> ```
> @id = Animals.item
> 
> ```
> 
> class Animals  
> def item
> 
> The Animals should be a class and the item should be a method.  
> but when I try to run this it gives: undefined method `item’ for  
> Animals:Class

As Fred pointed out, you have defined an _instance_ method, but you are  
using it like a _class_ method.

Class methods are methods invoked on a class and defined like this (not  
the reference to “self” which here stands for the class itself):

class Animals  
def self.item  
return 2  
end  
end

These methods are independent of any particular Animal (known as  
instances). They are designed to allow you to provide functionality  
about the class itself, such as creating new instances, etc. You call  
them like this:

Animals.item

Instance methods are methods invoked on a particular item of a class (an  
instance of that class). These allow you to manipulate or investigate  
specific instances of your class and are defined like this:

class Animals  
def item  
return 2  
end  
end

You call this on a particular Animal. You might get this from another  
method, or create a new one yourself:

my\_animal = Animals.new

Then you can call this instance method:

my\_animal.item

These concept of class and instance and the distinction between the  
methods for each are at the core of Object Oriented Programming.
