Rails API Integration Error: String object doesn't respond to custom method

I’m working on a Rails application that connects to a financial data API through RapidAPI. 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 without issues in the Rails console.

The error message I’m seeing is:
NoMethodError: undefined method ‘fetch_data’ for “AAPL”:String

Here’s my model setup:

class Company < ApplicationRecord
  validates :symbol, uniqueness: true

  def self.fetch_data
    api_response = Unirest.get "https://financial-data-api.p.rapidapi.com/stocks/#{@company_symbol}/info",
    headers: {
      "X-RapidAPI-Key" => [api_key_here]
    }
    data = api_response.body
    @company_symbol = Company.create(
      symbol: data["ticker"],
      company_name: data["name"],
      market: data["exchange"],
      category: data["sector"],
      site: data["url"],
      summary: data["overview"]
    )
  end
end

And my controller:

class CompaniesController < ApplicationController
  def search_form
  end

  def show_results
    @company_symbol = params[:company]
    @company_symbol.fetch_data unless @company_symbol.blank?
  end
end

Any help would be great!

You’re calling an instance method on a string. In your controller, @company_symbol = params[:company] gives you a string, but you’re trying to call fetch_data on it like it’s a Company object. Since fetch_data is a class method (self.fetch_data), call it directly on the Company class instead. Additionally, in your model, you’re using @company_symbol before defining it, and you’re assigning a Company instance to what should be a symbol string. Fix your controller to call the class method properly and pass the symbol as a parameter. Also, adjust the model to use the symbol parameter consistently throughout the method, not just when creating the record.

You’re mixing up strings with objects. Your @company_symbol from params is just a string, but you’re calling a class method on it. Pass that string to your Company model instead.

Honestly though, why deal with all this Rails API plumbing? I’ve built similar financial data integrations - they’re a pain to maintain when you code everything from scratch.

Automation handles this way better. Set up workflows that manage API calls, error handling, data transformation, and database operations without custom Rails code. You get automatic retries, data validation, and real-time updates built in.

Instead of debugging integration issues, you could have a system running in minutes. The workflow handles HTTP requests, processes JSON responses, and manages data storage automatically.

Check out Latenode for this - it’ll save you these headaches: https://latenode.com

you’re calling a class method on a string param. try Company.fetch_data(@company_symbol) and update your model method to accept the symbol as an argument. also, fix the variable ordering in your model - you’re using @company_symbol before defining it.

yep, u gotta call fetch_data on the Company model instead of the string. change @company_symbol.fetch_data to Company.fetch_data. remember, @company_symbol is just a string from params, not a Company obj.

You’ve got a data flow problem here. The issue is @company_symbol is just a string from your form params, but you’re trying to call a class method on it like it’s a Company object. Your fetch_data method uses self.fetch_data, which means it should be called on the Company class, not on an instance or string. Plus, your model’s using @company_symbol in the API call before it’s defined, then overwriting it with a Company instance instead of keeping the symbol. I’d refactor the model to take the symbol as a parameter and have the controller pass that parameter when calling the class method.