rubyclass-methodnomethoderror

Difficulty calling a class method in rails. Have other class methods that work fine


So I am trying to write a class method to send a csv file using a mailer class. I think I have that logic all worked out and the issue I am experience is a NoMethodError. I have defined the method within my Order class (see below):

class Order < ApplicationRecord

def self.to_csv
    attributes = %w{ id customer_name, customer_email, street_address, city, state, zip_code, number_books }
    orders = Order.where(Date.today.all_day)

    CSV.generate(headers: true) do |csv|
      csv << attributes

      orders.each do |order|
        csv << attributes.map { |attr| order.send(attr) }
      end
    end
  end
end

I am simply trying to call this method in the console to see what kind of output it gives me.

I get the following error:

NoMethodError: undefined method `to_csv' for #<Class:0x00007fb7ef8a7560>
Did you mean?  to_s
from /Users/mymac/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'

I have also tried writing any class methods that do something simple such as printing a word to the console and I get the same error. I have restarted my rails server, but I don't think I should even have to do this??

Any help is apprecitated. Having an issue with something I have done a million times without issue is extremely frustrating.


Solution

  • Rails normally reloads anything in app/ and config/routes.rb when they're changed but there are circumstances under which the class doesn't properly reload and a cached version of the old version is stuck. This is most often caused by Spring which tries to keep up-to-date but can slip out of sync on occasion.

    The usual fix is:

    spring stop
    

    You can also disable it if it's causing a lot of hassles for you. The Rails start-up time might be slower, but it might not be an issue for you.