Wednesday, September 5, 2012

Execute raw query and manual connection

Hello Rubies,
           Some time when we have huge query that it will be easy to use RAW SQL compare to ruby's active record query.

ActiveRecord::Base.connection.query <<-END
   /* Raw sql query */
  Select * from table where conditions
END

Custom connection and query

env = RAILS_ENV
config = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(config[env])

schemas = ActiveRecord::Base.connection.select_values("select * from pg_namespace where nspname not in ('public','information_schema') AND nspname NOT LIKE 'pg%'").inspect

No comments:

Post a Comment