# Creating Methods in the Model?

**URL:** <https://www.ruby-forum.com/t/creating-methods-in-the-model/165368>\
**Category:** Rails\
**Created:** [April 13, 2009, 6:27am UTC](https://www.ruby-forum.com/t/creating-methods-in-the-model/165368 "2009-04-13T06:27:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickfaraday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nickfaraday](https://www.ruby-forum.com/u/nickfaraday)\
**Post date:** [April 13, 2009, 6:27am UTC](https://www.ruby-forum.com/t/creating-methods-in-the-model/165368/1 "2009-04-13T06:27:33Z")

</div>

Hey Everyone.

First day diving in to ROR and Ruby…

My question is how do you define methods with in the model class and/or  
should I even be doing this?

It is my understanding that you should put as much of the business logic  
into the model as possible. I want to do some data manipulation before  
I things are submitted to the database

IE (Create a variable out of two submitted via form, as well as some  
other things.)

Should this all be done in the model? If so how do I declare and call  
these methods?

def method\_name  
def self.method\_name  
def class\_name.method\_name

don’t seem to work for me?

should I define them in the model and call them in controller? This  
doesn’t seem right to me?

Any Help would be appropriated.

-N

---

<div class="post-metadata">

**Author:** ![nickfaraday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nickfaraday](https://www.ruby-forum.com/u/nickfaraday)\
**Post date:** [April 13, 2009, 8:22am UTC](https://www.ruby-forum.com/t/creating-methods-in-the-model/165368/2 "2009-04-13T08:22:10Z")

</div>

On Mon, Apr 13, 2009 at 6:27 AM, Nick Faraday  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I things are submitted to the database
> 
> don’t seem to work for me?
> 
> should I define them in the model and call them in controller? This  
> doesn’t seem right to me?
> 
> Any Help would be appropriated.
> 
> ## -N

Yes you should be defining methods inside your model.  
Models are just classes and so you create methods just like any class.

To expand on your example of a ‘variable’ of two submitted via form:

class User \< ActiveRecord::Model  
def name  
“#{firstname} #{lastname}”  
end  
end

This gives you method which will combine firstname and lastname (which  
would be in the database) into a name

Usage:  
user = User.new(:firstname =\> ‘John’, :lastname =\> ‘Doe’)  
user.name #=\> “John D.”

There is nothing wrong with defining these and calling them from a  
controller, or view and not sure why that doesn’t seem right to you?  
You mention that the methods you have tried don’t work for you, if  
you’re still battling, give us some code to look at.

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![nickfaraday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nickfaraday](https://www.ruby-forum.com/u/nickfaraday)\
**Post date:** [April 13, 2009, 10:37am UTC](https://www.ruby-forum.com/t/creating-methods-in-the-model/165368/3 "2009-04-13T10:37:15Z")

</div>

This post should help

[http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model](http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model)

On Apr 13, 11:20 am, Andrew T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nickfaraday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nickfaraday](https://www.ruby-forum.com/u/nickfaraday)\
**Post date:** [April 14, 2009, 5:16am UTC](https://www.ruby-forum.com/t/creating-methods-in-the-model/165368/4 "2009-04-14T05:16:45Z")

</div>

On Tue, Apr 14, 2009 at 2:54 AM, Nick Faraday  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> return :permalink  
> end
> 
> end
> 
> The reason I don’t want to call it in the controller is there is no  
> reason to (right?), other than I can’t figure out how to call a  
> self.method in ruby. It is just one more line of code in the controller  
> that I don’t need.
> 
> Is this the right way of thinking?

Don’t use pseudo-ruby, just write ruby otherwise we can’t tell if  
you’re making a mistake.  
Don’t create a variable called permalink, create a method called  
permalink

class User \< ActiveRecord::Base  
validates…

def permalink  
#Do what you need to create the permalink and return it  
end  
end

There is no reason to validate the permalink because you’re creating  
it dynamically. Just validate the presence of name and city so you  
have both available to build your permalink  
Now whenever you call user.permalink, you’ll get what you need.

self.method creates a method on the User object so that you get  
User.method instead of user.method (which is called on the instance of  
the object)

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![nickfaraday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nickfaraday](https://www.ruby-forum.com/u/nickfaraday)\
**Post date:** [April 14, 2009, 2:54am UTC](https://www.ruby-forum.com/t/creating-methods-in-the-model/165368/5 "2009-04-14T02:54:54Z")

</div>

Ok, still not getting it…

Here is what I would like to do: (pseudo-ruby)

class User \< ActiveRecord::Base

# Input from user

validates\_presence\_of :name, :message =\> “Your name can’t be blank”  
validates\_presence\_of :city

#User defined Method  
:permalink =\> create\_permalink :name, :city

validates\_presence\_of :permalink

#Method Definition  
def create\_permalink(name, city)  
… permalink code  
return :permalink  
end

end

The reason I don’t want to call it in the controller is there is no  
reason to (right?), other than I can’t figure out how to call a  
self.method in ruby. It is just one more line of code in the controller  
that I don’t need.

Is this the right way of thinking?
