控制器
class QueriesController < ApplicationController
before_action :authenticate_user!
def new
@query = Query.new
end
def create
@query = Query.new
@query.query_string = params[:query][:query_string]
respond_to do |format|
format.js{}
end
end
end
模型
class Query < ActiveRecord::Base
include ActiveModel::Model
attr_reader :query_result, :query_string
def query_string=(query_str)
@query_string = query_str
establish_connection :readonly_db
@query_result = ActiveRecord::Base.connection.execute(query_str)
end
end
schema.rb
ActiveRecord::Schema.define(version: 20151029054443) do
create_table "readonly_dbs", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name", limit: 255
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
end
的database.yml
readonly_db:
adapter: mysql2
encoding: utf8
reconnect: false
database: #somedatabase
pool: 5
username: readonly_user
password: password
socket: /var/run/mysqld/mysqld.sock
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: query_app_development
test:
<<: *default
database: query_app_test
production:
<<: *default
database: query_app_production
username: query_app
password: <%= ENV['QUERY_APP_DATABASE_PASSWORD'] %>
这是我正在创建的应用程序,如查询浏览器。
但它抛出一个错误,说“Mysql2 :: Error:Table'query_app_development.queries'不存在:显示来自queries
的完整字段”
“QueriesController中的ActiveRecord :: StatementInvalid #new”