Topic: Class method?

I have a class method (I believe, Im failry new to this) and I am trying to access it in my controller.  I can get it to work in the rails console, but in rails it gives me an argument error. 


Inventory Model =>

  def return_date(repair)
    if repair
      number = self.group.default_loan_period
    else
      number = self.group.repair_loan_period
    end
    number.business_days.after(Time.now)    
  end

Controller =>

inventory = Inventory.find_by_asset_number(params[:asset])
    if inventory
        date =  inventory.return_date(true)
        @transaction_detail = TransactionDetail.new(:transaction_id => params[:transaction_id],
                                                    :inventory_id => inventory.id,
                                                    :due_back_at => date
                                                    )

When I type the following in my rails console it works.  When I try it in the rails app, it says "wrong number of arguments (0 for 1)"  I checked that inventory in my controller has an object via outputting the ID in the log.    I type this in the rails console and it works

Console =>

i = Inventory.first
i.return_date(false)
=> Wed, 08 Aug 2012 23:57:58 EDT -04:00 
i.return_date(true)
 => Thu, 02 Aug 2012 23:58:35 EDT -04:00 

Last edited by todd.sanders79 (2012-08-02 00:04:08)

Re: Class method?

Could you post the full stack trace of the error you get when calling the method from the controller (see it either in your console or in the corresponding *.log file.

Second point, no need to prefix with self:

number = self.group.default_loan_period

by default the call will be made on the current object, prefix only when you assign a value:

number = group.default_loan_period
#but
self.group.default_loan_period = some_value

Last edited by Javix (2012-08-02 03:27:31)

Re: Class method?

Sorry it took so long to get back.  Im not sure how, but it was working the next day.  Perhaps I worked to late.  I'm still trying to reproduce the error I was getting the other night though to learn from it.

Thanks for your reply!