Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails service object returning 'No Method error'

To begin: This is my first attempt at getting business logic out of the Model/controller space. Here is some initial logic I'm trying to abstract. The path is app/services/Date_calc.rb.

class Date_calc
  require User
  require Report

  def months(range)
    User.first.reports.order("report_month desc").limit(range).pluck(:report_month)
  end
end

In my application I have two models, User and Reports. User has_many Reports. The reports table has a field called report_month.

Calling Date_calc.months(6) in the Rails console returns: TypeError: no implicit conversion of Class into String.

My intended response was an array of dates, e.g. ["01/01/2013", "01/02/2013", "01/03/2013", ... ].

I'm not really sure what I'm doing wrong here.

like image 741
greyoxide Avatar asked Feb 25 '26 23:02

greyoxide


1 Answers

In addition to the require lines lacking quote marks, you are calling an instance method on a class. You either need to instantiate the Date_calc like this

months = Date_calc.new.months(6)

or make it a class method like this

def self.months(range)...
like image 198
msergeant Avatar answered Feb 28 '26 14:02

msergeant