# My own methods on model + "extends" problem?

**URL:** <https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141>\
**Category:** Rails\
**Created:** [November 14, 2007, 2:21am UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141 "2007-11-14T02:21:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 14, 2007, 2:21am UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/1 "2007-11-14T02:21:23Z")

</div>

Hello.

I have a UsersController and a Model called User with a “new” method,  
like this:

class User \< ActiveRecord::Base

```
  def test
       return 1 #I can do anything and return any data, the

```

problem continues.  
end

end

The problem is that I cannot access my “test” function from the  
controller. It says that the method doesn’t exists.

class UsersController \< ApplicationController

```
     def index
              render :text => User.test # Produces the error.
     end

```

end

Additionally, I cannot access the mehod increase! and other methods of  
the ActiveRecord::Base class, that is extended by the User class.  
Shouldn’t it be accessible?

The User.methods doesn’t display the method I created and I cannot  
access it. However, other very used methods, like “find” are not shown  
by User.methods, but they are accessible.

Anyone can explain me how these things work?

Thank you very much.

---

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 14, 2007, 2:25am UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/2 "2007-11-14T02:25:28Z")

</div>

On 13-Nov-07, at 8:16 PM, mbleonardo wrote:

> problem continues.  
> render :text =\> User.test # Produces the error.  
> by User.methods, but they are accessible.
> 
> Anyone can explain me how these things work?
> 
> Thank you very much.

It’s an issue of Class methods, version Instance methods.

“def test” defines an instance method - @user.test

“def self.test” defines a Class method - User.test

In your example you defined an instance method, and tried to call a  
Class method (User.test).

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 14, 2007, 2:25am UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/3 "2007-11-14T02:25:28Z")

</div>

Correction: The method is increment! not increase!

---

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 15, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/4 "2007-11-15T15:17:02Z")

</div>

Thank You both 😉

---

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 14, 2007, 4:12am UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/5 "2007-11-14T04:12:31Z")

</div>

Just to expand a little:

The #find method is defined at  
[Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001005),  
but the “find\_by…” class of methods is defined dynamically in the  
#missing\_method method that is part of Ruby and used to good advantage  
in Rails. That is why Rails is able to implement such a broad range  
of #find\_by\_\* model methods - they are implemented dynamically.

As Jodi says, your case is tripped up because of the difference  
between class methods and instance methods.

So if you did the following (note the call to the #new method which  
returns an instance of the User object:  
def index  
render :text =\> User.new,test  
end

I suspect you would get the right response.

Cheers, --Kip

---

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 15, 2007, 3:24pm UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/6 "2007-11-15T15:24:04Z")

</div>

… And what about the “increment” method? How to use it?

Thanks again

---

<div class="post-metadata">

**Author:** ![mbleonardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@mbleonardo](https://www.ruby-forum.com/u/mbleonardo)\
**Post date:** [November 15, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/my-own-methods-on-model-extends-problem/120141/7 "2007-11-15T15:32:55Z")

</div>

Ex:

If I use this:

class RomsController \< ApplicationController

```
     def login
           @user = user.find(5)

           @user.increment! "visits"
       end

```

end

Returns the error “invalid method increment!”, but, when I use in the  
view \<%=@rom.methods %\>, it displays the increment! method.

I think I haven’t gotten the correct way to send variables to the  
view yet… ☹

Thanks again
