0

I have a method in my view helper directory that I am trying to use within a model but i keep getting a undefined method error. I cannot figure out what i am doing wrong.This is my module.

module StbHelper
def gen_csv(stbs)
    CSV.generate do |csv|
        csv << [
            'param1',
            'param2'
        ]
        stbs.each do |stb|
            health_check = stb.stb_health_checks.last
            csv << [
                'value1',
                'value2'
            ]
        end
    end
end

This is the class i want to use the method in.

    require 'stb_helper'
class Stb < ActiveRecord::Base

    def self.get_notes_data
        .
        .
        .
    end

    def self.update
        .
        .
        .
    end

    def self.report(options={})
        csv_file = nil
        if options == {}
           ########################################
           # This is the line that throws the error
            csv_file = StbHelper.gen_csv(Stb.all)
           #######################################
        else
            stbs = []
            customers = List.where(id: options[:list])[0].customers
            customers.each do |customer|
                customer.accounts.each do |account|
                     stbs += account.stbs
                end
            end
            csv_file = StbHelper.gen_csv(stbs)
        end
    end
end
bo-oz
  • 2,842
  • 2
  • 24
  • 44
Abass Sesay
  • 833
  • 10
  • 18
  • 1
    As you already said in your question, helpers are for views. In order to use it in your model [see this question](http://stackoverflow.com/questions/489641/using-helpers-in-model-how-do-i-include-helper-dependencies), [or this tutorial](http://makandracards.com/makandra/1307-how-to-use-helper-methods-inside-a-model) – JuanM. Nov 30 '15 at 15:45
  • 1
    Short answer: using a view helper in the model. View helpers are for views. It looks like you just want a simple utility library/class/module. – Dave Newton Nov 30 '15 at 15:58
  • These comments have sort of point me in the right direction. I decided to move the method to the model and make it a class level method. Everything works now – Abass Sesay Nov 30 '15 at 16:30

1 Answers1

0

You've defined a module, that doesn't require instantiation. You should be able to use it without the StbHelper part (as long as you require the module in the document):

def self.report(options={})
    csv_file = nil
    if options == {}
       ########################################
       # This is the line that throws the error
        csv_file = gen_csv(Stb.all)
       #######################################
    else
        stbs = []
        customers = List.where(id: options[:list])[0].customers
        customers.each do |customer|
            customer.accounts.each do |account|
                 stbs += account.stbs
            end
        end
        csv_file = gen_csv(stbs)
    end
end

But you shouldn't use a helper for this, you can create a normal module and require it the same way.

Edit: Save the module in a new folder called app/modules (and restart the server), save a file called stb_helper.rb with the contents of your module:

module StbHelper
def gen_csv(stbs)
    CSV.generate do |csv|
        csv << [
            'param1',
            'param2'
        ]
        stbs.each do |stb|
            health_check = stb.stb_health_checks.last
            csv << [
                'value1',
                'value2'
            ]
        end
    end
end
bo-oz
  • 2,842
  • 2
  • 24
  • 44
  • yeah, same thing as a module right? Only penalty this way, would be the unnecessary availability in the view. – bo-oz Nov 30 '15 at 15:58