I’m working with a financial data API through RapidAPI in my Rails application. When I submit my search form, I get an error saying the method doesn’t exist on the string object, even though the same code runs perfectly in the Rails console.
Error: NoMethodError - undefined method ‘fetch_data’ for “AAPL”:String
Here’s my model:
class Company < ApplicationRecord
validates :symbol, uniqueness: true
def self.fetch_data
api_response = Unirest.get "https://financial-api.example.com/companies/#{@ticker}/info",
headers: {
"X-RapidAPI-Key" => [api_key]
}
data = api_response.body
@ticker = Company.create(
symbol: data["ticker"],
company_name: data["name"],
market: data["market"],
category: data["sector"],
url: data["homepage"],
details: data["summary"]
)
end
end
And my controller:
class CompaniesController < ApplicationController
def search_form
end
def show_results
@ticker = params[:company]
@ticker.fetch_data unless @ticker.nil?
end
end
Yeah, everyone’s right about the class method issue, but this whole approach will bite you later. You’re manually building API integrations and handling all the error cases yourself.
I hit the same wall when we pulled market data from multiple sources. Started with manual Rails methods like yours, then realized I kept recreating the same patterns.
You need proper API workflow automation. Set up data fetching as automated workflows that handle retries, rate limiting, and error responses without cluttering your Rails code.
Your controller should just trigger the workflow and get clean data back. No more debugging scope issues or method errors - the heavy lifting happens outside your app.
We moved all external API calls to automated workflows and eliminated these bugs completely. Rails code stays clean and focused.
Check out Latenode for this. It handles API complexity so your Rails app just works with the data.
Had this exact issue last month building a stock tracker. You’re mixing up class methods and instance variables. Your fetch_data is defined as self.fetch_data (making it a class method), but inside you’re using @ticker which isn’t defined in that scope. Refactor it to accept the ticker symbol as a parameter: def self.fetch_data(symbol). Then call it from your controller with Company.fetch_data(@ticker). Also, you’re assigning the created company record back to @ticker, which overwrites your string parameter - use a different variable name.
Had this exact issue with third-party APIs. You’re calling an instance method on a string - params[:company] comes in as “AAPL” but you’re treating it like an Active Record object. Since you defined fetch_data with self, it’s a class method. Call it on Company directly. But there’s a bigger problem with your model logic. You’re using @ticker for both input and output, and instance variables don’t work in class method scope anyway. I’d restructure it to pass the symbol explicitly and return the company object instead of reassigning the same variable.
classic rails mistake. ur calling an instance method on a string when fetch_data is a class method. fix ur controller to use Company.fetch_data and pass the ticker as a param. that @ticker variable in ur model won’t work in class method scope anyway.
You’re calling a class method on a string, which won’t work. Your fetch_data method belongs to the Company class, but @ticker is just a string like “AAPL”. When you write @ticker.fetch_data, you’re trying to call the method on that string - and strings don’t have a fetch_data method. Fix this by passing the ticker as a parameter: Company.fetch_data(@ticker). Also, your model has scope issues - @ticker isn’t available inside the class method anyway. Pass the symbol explicitly and clean up that API call.