# Unable to call method in same class

**URL:** <https://www.ruby-forum.com/t/unable-to-call-method-in-same-class/97190>\
**Category:** Rails\
**Created:** [December 11, 2005, 5:59pm UTC](https://www.ruby-forum.com/t/unable-to-call-method-in-same-class/97190 "2005-12-11T17:59:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 11, 2005, 5:59pm UTC](https://www.ruby-forum.com/t/unable-to-call-method-in-same-class/97190/1 "2005-12-11T17:59:22Z")

</div>

Hi,

I have the following two methods in my class:

def self.decrease\_credits(amount, user\_id)  
user = User.find(user\_id)  
company = Company.find(user.company\_id)  
credits\_new\_saldo = company.credits - amount  
company.update\_attribute(:credits, credits\_new\_saldo)  
end

def self.available\_credits(user\_id)  
user = User.find(user\_id)  
company = Company.find(user.company\_id)  
return company.credits  
end

They both work, but since the code of the second method is the same as  
the first three lines of the method self.decrease\_credits, I would like  
to rewrite it as:

def self.decrease\_credits(amount, user\_id)  
credits\_new\_saldo = available\_credits(user\_id) - amount  
company.update\_attribute(:credits, credits\_new\_saldo)  
end

But then I get the error that available\_credits is not known in the  
class. Why is this not working and what is the right way? All help is  
greatly appreciated. Thanks.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 11, 2005, 8:02pm UTC](https://www.ruby-forum.com/t/unable-to-call-method-in-same-class/97190/2 "2005-12-11T20:02:34Z")

</div>

Hi Andreas,

thanks for your help. Really, really helpful. Too complicated you say,  
well and I thought I was doing it the simple way! Thanks for pointing me  
to instance methods, a piece of Rails I will be exploring and definitely  
using more and more in the future. It is really ridiculously easy, wow!  
😃 Thanks.

Nick

Andreas S. wrote:

> But all of this seems way too complicated. Why don’t you use instance  
> methods?
> 
> class User  
> def available\_credits  
> self.company.credits  
> end
> 
> def decrease\_credits(amount)  
> credits\_new\_saldo = self.available\_credits - amount  
> self.company.update\_attribute(:credits, credits\_new\_saldo)  
> end  
> end

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 11, 2005, 8:16pm UTC](https://www.ruby-forum.com/t/unable-to-call-method-in-same-class/97190/3 "2005-12-11T20:16:52Z")

</div>

Andreas S. wrote:

> Nick S. wrote:
> 
> > They both work, but since the code of the second method is the same as  
> > the first three lines of the method self.decrease\_credits, I would like  
> > to rewrite it as:
> > 
> > def self.decrease\_credits(amount, user\_id)  
> > credits\_new\_saldo = available\_credits(user\_id) - amount
> 
> self.available\_credits(user\_id)

That’s wrong, it should be:  
self.class.available\_credits(user\_id)

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [December 11, 2005, 6:33pm UTC](https://www.ruby-forum.com/t/unable-to-call-method-in-same-class/97190/4 "2005-12-11T18:33:26Z")

</div>

Nick S. wrote:

> Hi,
> 
> I have the following two methods in my class:
> 
> def self.decrease\_credits(amount, user\_id)  
> user = User.find(user\_id)  
> company = Company.find(user.company\_id)

Why not “company = user.company”?

> They both work, but since the code of the second method is the same as  
> the first three lines of the method self.decrease\_credits, I would like  
> to rewrite it as:
> 
> def self.decrease\_credits(amount, user\_id)  
> credits\_new\_saldo = available\_credits(user\_id) - amount

self.available\_credits(user\_id)

But all of this seems way too complicated. Why don’t you use instance  
methods?

class User  
def available\_credits  
self.company.credits  
end

def decrease\_credits(amount)  
credits\_new\_saldo = self.available\_credits - amount  
self.company.update\_attribute(:credits, credits\_new\_saldo)  
end  
end
