Any ideas why this doesn't work, I get a NoMethodError
when I try and run the code below via rails runner
.
Maybe I am calling the rails runner incorrectly, sorry new to Rails!
File location:
/app/scripts/data_import.rb
Command:
rails runner -e development DataImport.say_hi
Error:
undefined method `say_hi' for DataImport:Class (NoMethodError)
Code:
class DataImport
def say_hi
puts "hi"
end
end
You are calling an instance method on the class, so it's undefined. Try making your method a class method instead:
class DataImport
def self.say_hi
puts "hi"
end
end
Change it to
class DataImport
def self.say_hi
puts "hi"
end
end
Since you're accessing it as a class method and not a method on an instance of the class, you need the self
to declare the method as a class method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With