Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call helper method from model

For my Predictions model I created a method in predictions_helper.rb like this:

module PredictionsHelper

  def time_in_date(time, date)
    time.between(date..date+1.day)
  end
end

However, when I call it like this:

time_in_date(le.time, date)

I get the following error:

NoMethodError:
       undefined method `time_in_date' for PredictionsHelper:Module
like image 925
octavian Avatar asked Oct 31 '25 02:10

octavian


2 Answers

It seems like the easiest thing to do would be to include your module in your Predictions class

class Prediction < ActiveRecord::Base
  include PredictionsHelper
end

That will give you access to time_in_date

like image 103
John Naegle Avatar answered Nov 02 '25 23:11

John Naegle


The predictions_helper is within scope of the Predictions model, however, since you've put it inside of a module, you'll need to include that module.

So:

include PredictionsHelper
# code...

or

PredictionsHelper::time_in_date(le.time, date)
like image 32
Dan Avatar answered Nov 02 '25 21:11

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!