简体   繁体   中英

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!

在你的模型中,名称是Tab,你在控制器Tabs中使用即错误,请使用Tab.all或Tab.find_by_sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM