Forum: Ruby on Rails Unable to call method in same class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2005-12-11 17:59
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
3a83969376c805ef5b6042191fdb0ff3?d=identicon&s=25 Andreas S. (andreas)
on 2005-12-11 18:33
Nick Snels 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
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2005-12-11 20:02
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!
:D Thanks.

Nick


Andreas Schwarz 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
3a83969376c805ef5b6042191fdb0ff3?d=identicon&s=25 Andreas S. (andreas)
on 2005-12-11 20:16
Andreas Schwarz wrote:
> Nick Snels 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)
This topic is locked and can not be replied to.