Mike S
Mike S

Reputation: 25

Connecting to a separate mysql database in Rails

I am trying to connect to a separate mysql database than what my rails app is on. I am trying to connect to a database of guitar tabs so that users can search for specific songs.

I have my database.yml configured to:

     tabs:
      adapter: mysql2
      encoding: utf8
      database: (dbname)
      username: (username)
      password: (pass)
      host: hostname.rds.amazonaws.com
      port: 3306

So far I have tab.rb as my model:

    class Tab < ActiveRecord::Base

      self.abstract_class = true
      establish_connection ('tabs')

    end

finally, my controller

  class TabController < ApplicationController

    def listTabs
    @tabs = Tabs.all

    respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @tabs }
    end
end

def showTabs

    @tabs = Tabs.find_by_sql "SELECT * FROM gp"

          respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @statuses }
      end
    end


 end

I'm new to rails and I really want to get this to work. If you are able to help me just run the query "SELECT * From gp" and display it in my view, I will love you forever.

Thanks for your help!

Upvotes: 0

Views: 55

Answers (1)

HarsHarI
HarsHarI

Reputation: 911

in your model the name is Tab and you use in controller Tabs i.e is wrong, please use Tab.all or Tab.find_by_sql

Upvotes: 2

Related Questions