# Model methods when to use self

**URL:** <https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898>\
**Category:** Rails\
**Created:** [May 11, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898 "2007-05-11T13:46:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [May 11, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898/1 "2007-05-11T13:46:13Z")

</div>

I am extending my model with some methods. My question is when to and  
when not to define the method with self.

example

def sales\_value  
… all kinds of calculations  
end

OR

def self.sales\_value  
… all kinds of calculations  
end

---

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [May 11, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898/2 "2007-05-11T18:53:54Z")

</div>

The shortest (but not the most correct one) way to describe the  
difference is to say that def meth1; end is an object method while def  
self.meth2; end is a class method, and you cal them differently:

a = Foo.new  
a.meth1  
Foo.meth2

But there’s more than that

As for Active Records it depends whether you want a method for an  
ActiveRecord instance or for the class

Btw these are Ruby basics, find a good Ruby book/tutorial.

Cheers,  
Yury

On 5/11/07, bitterbal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OR
> 
> def self.sales\_value  
> … all kinds of calculations  
> end
> 
> >

–  
Best regards,  
Yuri L.

---

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [May 12, 2007, 12:18am UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898/3 "2007-05-12T00:18:26Z")

</div>

bitterbal wrote:

> I am extending my model with some methods. My question is when to and  
> when not to define the method with self.
> 
> example
> 
> def sales\_value  
> … all kinds of calculations  
> end
> 
> OR
> 
> def self.sales\_value  
> … all kinds of calculations  
> end

My answer is the same as Yuri’s, just different words.

If sales\_value effects or returns a different value for each row in the  
table don’t use self. You would then do this for example

sale = Sale.find(id)  
puts “The sales value of sale #{id} is: #{sale}”

If sales\_value combines data from some or all rows in the table use  
self.sales\_value. You might then do this:

sales = Sale.sales\_value #note there is no find here  
puts “the value of all sales is #{sales}”

---

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [May 12, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898/4 "2007-05-12T14:20:28Z")

</div>

> I am extending my model with some methods. My question is when to  
> and when not to define the method with self.

Ruby 101 :

```
class String
  def shuffle
    split(//u).sort_by { rand }.join('')
  end

  CHARS = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  def self.random( len )
      newpass = ""
      1.upto(len) { |i| newpass << CHARS[rand(CHARS.size-1)] }
      return newpass
  end
end

puts String.random(10)
puts "abcdef".shuffle

```

## Alain R.

[http://blog.ravet.com/](http://blog.ravet.com/)

---

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [May 12, 2007, 12:57am UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898/5 "2007-05-12T00:57:38Z")

</div>

Maybe this will help you.

When you are writing model code you are writing a class. That is  
“working class” not “learnstitute class.”

Think of “blue collar” and “white collar” workers. People of the  
“blue collar” class work a certain way that is difference from a  
“white collar” worker. So you may have people (instances) of “blue  
collar” and “white” collar workers.

So you have a Class and some Objects of each class. Both the Class  
and the Objects can have methods (referred to as class and instance  
methods respectively).

When you are writing your code you are writing a class represented in  
the def as “self.”

So…

def self.my\_class\_method

# Can be called on the Class or instances of the class

end

def my\_instance\_method

# Is only available to instances of the class

end

Calling them…

BlueCollarWorker.my\_class\_method # called on the class  
a\_person\_object.my\_instance\_method #called on the instance.

On May 11, 6:18 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bitterbal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/838e76/32.png) [@bitterbal](https://www.ruby-forum.com/u/bitterbal)\
**Post date:** [May 13, 2007, 8:39am UTC](https://www.ruby-forum.com/t/model-methods-when-to-use-self/103898/6 "2007-05-13T08:39:57Z")

</div>

Thanks. great example that makes it clear for me.

On May 12, 12:18 am, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
