# Hard time understanding the differences between "def self.fo

**URL:** <https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943>\
**Category:** Rails\
**Created:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943 "2006-08-15T14:20:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@blackball](https://www.ruby-forum.com/u/blackball)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943/1 "2006-08-15T14:20:50Z")

</div>

Hi guys,

I’m having a little trouble understanding the differences and knowing  
when to use “def foo” and “def self.foo” in my models. I don’t quite  
understand them and was hoping someone could explain or give me  
examples on how to use the “self.” properly.

For example, I had “def foo” in my model “Account” and in one of my  
views I tried doing “\<%= Account.foo %\>” but it didn’t work, but when  
I changed it to “def self.foo” it did. But I don’t know why.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![blackball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@blackball](https://www.ruby-forum.com/u/blackball)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943/2 "2006-08-15T14:20:51Z")

</div>

Hey Rob,

The difference is between a Class and an Object. (btw, this is basic  
object oriented stuff you’ll find in any reference)

Examples work best;

Dog is a Class, and your dog Fido is an Object (a specific instance  
of a Dog).

So Dog.new references the Class method new, which gives you a new dog.

fido = Dog.new

and on fido, you can do specific dog things.

dog.sniff!  
dog.eat(food)

etc.

in terms of ruby, you define a Class method as:  
def self.new  
end

and a object (aka instance) method  
def sniff!  
end

hope that helps Rob.  
(I tried to work in some ‘mongrel’ references, but that started to  
look like a subclass of dog that complicated the matter)

cheers,  
Jodi

---

<div class="post-metadata">

**Author:** ![blackball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@blackball](https://www.ruby-forum.com/u/blackball)\
**Post date:** [August 15, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943/3 "2006-08-15T17:38:54Z")

</div>

Hi Jodi,

Thanks. So can you tell me if I understand this correctly?

Instance methods are defined without the “self.” and are accessible  
to all new class instances (objects)?

Class methods are defined with the “self.” and can be accessed by  
themselves using Class.method? and dont require a new class instance?

So like…

class Dog

def self.find\_owner (id)  
self.find\_by\_owner(id)  
end

def set\_new\_owner(owner)  
self.owner = owner  
end

end

so if i just wanted to find a random persons owner, i can just do…

owner = Dog.find\_owner(1)  
owner = Dog.find\_owner(5)

without creating any new objects / class instances… and if I want to  
create a new dog and give it a new owner’s name i can do…

fido = Dog.new(:name =\> “fido”, :owner =\> “rob”)  
fido.set\_new\_owner=“jerry”  
fido.save

Am I on the right track?

---

<div class="post-metadata">

**Author:** ![blackball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@blackball](https://www.ruby-forum.com/u/blackball)\
**Post date:** [August 15, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943/4 "2006-08-15T17:56:57Z")

</div>

Well, almost.  
find\_owner would be calling find\_by\_owner on the Dog class.  
So, it would return an array of Dog objects, not owner(s).

Your set\_new\_owner(owner) method is right, but there’s a better way.  
fido.owner = “jerry”  
fido.save

If you need to override the default accessor, you can do that:  
def owner=(new\_name)  
self.name = new\_name.capitalize # always store names capitalized  
end

Make sense?

– Wes

---

<div class="post-metadata">

**Author:** ![blackball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@blackball](https://www.ruby-forum.com/u/blackball)\
**Post date:** [August 15, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943/5 "2006-08-15T18:36:04Z")

</div>

Rob -

The find\_by\_owner Class method Wes mentions is provided to you by  
Rails - as well as a bunch of others per finders field (find\_by\_#  
{field\_name})

Plus rails gives you accessors (instance methods) for every column in  
your database. So fido.owner (fido.size, fido. color) will be  
automatically available with the following class definition:

class Dog \< ActiveRecord::Base  
end

you get a bunch of free instance and class methods - as long as you  
follow the rails naming conventions (tablename is ‘dog’, and has a  
column id : integer [primary key]).

Rob - I suggest you pickup one of the great rails tutorials. You’ll  
get a more complete picture than we can provide - http://  
[www.pragmaticprogrammer.com/title/rails1/](http://www.pragmaticprogrammer.com/title/rails1/) is a definitely worthwhile.

Jodi

---

<div class="post-metadata">

**Author:** ![blackball](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a5b964/32.png) [@blackball](https://www.ruby-forum.com/u/blackball)\
**Post date:** [August 15, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/hard-time-understanding-the-differences-between-def-self-fo/64943/6 "2006-08-15T19:00:07Z")

</div>

Thanks. I already knew that you can use find\_by\_fieldname with rails  
as well as getting field names automatically with fido.name,  
fido.owner, etc… I just used those as examples to see if I  
understood the difference between instance methods and class methods…
