Skip to content

Instantly share code, notes, and snippets.

@joseluistorres
Created May 24, 2016 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluistorres/05d5bb20b3f84692e469488ead554baa to your computer and use it in GitHub Desktop.
Save joseluistorres/05d5bb20b3f84692e469488ead554baa to your computer and use it in GitHub Desktop.
Run a custom query function from rails console to another DB
# whenever you need to do a custom query/function/sproc into another DB that is not development
app = ActiveRecord::Base.establish_connection(
adapter: "mysql",
host: "localhost",
username: "myuser",
password: "mypass",
database: "somedatabase"
)
# if you have already defined an entry in database.yml
# -------------- READ THIS TOO ---------------------------------
# use app = ActiveRecord::Base.establish_connection(:your_custom_db)
sql = <<-SQL
select * FROM whatever_table( 'somestring', 2016 );
SQL
app.connection.execute(sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment